-4

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.

  • 1
    Screenshots of the UI are great, screenshots of code or HTML are not. Please read why [a screenshot of code/HTML is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code/HTML/error message and properly format it instead. – JeffC Jan 29 '23 at 02:34
  • Restructuring your code something to below will work. driver.manage().window().maximize(); driver.get("https://baasithfazil.github.io/resume/"); WebElement elem = driver.findElement(By.cssSelector("a.nav-twitter")); elem.click(); – Srinivasu Kaki Jan 31 '23 at 01:58

3 Answers3

-1

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);
  • Implicit waits are a bad practice according to the Selenium lead and devs. Instead use `WebDriverWait`. – JeffC Jan 29 '23 at 02:35
  • @JeffC could be a bad practice but not wrong to use. Adding one statement would eliminate multiple webdriverwait statements in the code. The fact the WebDriver statement used after find element step returns the no element found exceptions. I have seen this issue number of times & adding implicit wait worked every time. – Srinivasu Kaki Jan 29 '23 at 05:11
-1

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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

You have used wrong selector/locator here. "twitter" is not tag name, Tag name is the value from less than symbol to the first space or greater than symbol. Words like div, p, a, span are the tag names.

So here a is the tag name and name, class, href are attribute which you can use while forming xpath or css locators. So you can create multiple locators here:

  1. By using class name attribute

    By.className("nav-twitter");
    
  2. By creating xpath using name attribute

    By.xpath("//a[@name='twitter']");]
    
  3. By creating xpath using class attribute

    By.xpath("//a[@class='nav-twitter']");
    
  4. By creating css locator using class attribute.

    By.cssSelector("a[class*='nav-twitter']");
    
  5. By using dot symbol in css selector with class name

    By.cssSelector("a.nav-twitter");
    

Similarly you can create css selector with name too. Since there are multiple tags, so you can not use a tag directly and If you want to use By.tagName("a") then it will click on first tag present on the page

Note: We use dot with class name and Hash (#) with Id name while forming css selectors.

  • none of these sytax works. dont know why. – Baasith Fazil Jan 30 '23 at 11:53
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 31 '23 at 09:10
  • @BaasithFazil I think you are unable to convey your question correctly. These are the correct syntax which can be formed for the mentioned part of HTML. And you were using absolutely wrong locator. – akansh singhal Jan 31 '23 at 12:03