Home > Mobile >  cant find the element using classname in selenium java
cant find the element using classname in selenium java

Time:01-29

I'am using selenium framework to test my own website. Im trying to click on specific icon which using anchor tag. I have java selenium code to click but couldn't click. Tried many xpath, css selectors, class name and names. but didn't worked. But can run the script and it is opening the chrome and navigating to entered domain but the clicking option is not workingThis the code of my website

above code I need to click nav-twitter class anchor option . which will create another tab in chrome to show my twitter page. but after running the app .it is only navigating to the page domain and nothing works.

Selenium Code Snippets

So, This is my code where I have added. until the maximize everything works but not the anchor tag

Error Image

This kind of error im getting when running the script in chrome. Please anyone let me know where I have been wrong here or is there are any way to make it happen. Thanks in advance.

CodePudding user response:

Your locator is slightly off. In your code you are looking for an element with tagName as twitter which doesn't exists. Instead you can use either of the following locator strategies:

  • Using cssSelector:

    WebElement navTwitter = driver.findElement(By.cssSelector("a.nav-twitter[name='twitter']"));
    navTwitter.click();
    
  • Using xpath:

    WebElement navTwitter = driver.findElement(By.xpath("//a[@class='nav-twitter' and name='twitter']"));
    navTwitter.click();
    

CodePudding user response:

Whenever you are initiating webdriver make sure to add implicit wait statement, so it can wait for sometime before looking for objects. Add below statement right after chromedriver initialization and your code should work without any issues.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  • Related