2

I have received this error for several times:

Unable to locate element: {"method":"xpath","selector":"//li[@id="tablist1-tab3"]"}

Code that I have used is:

options.addArguments("--headless");
options.addArguments("window-size=1200x900");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement tab = driver.findElement(By.xpath("//li[@id=\"tablist1-tab3\"]"));
tab.click();

Can someone help me with this error?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Test
  • 47
  • 1
  • 6

3 Answers3

2

You need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li#tablist1-tab3"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id=\"tablist1-tab3\"]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

WebElement tab = driver.findElement(By.xpath('//li[@id="tablist1-tab3"]')); try this

Agnia
  • 84
  • 7
  • You can inspect this element on website and in source code by clicking right mouse click you can copy exact xpath. – Agnia Nov 25 '20 at 21:15
  • this is the xpath : //*[@id="tablist1-tab3"]. I m at the begin and I had this error for several times. – Test Nov 25 '20 at 21:18
  • WebElement tab = driver.findElement(By.xpath('//*[@id="tablist1-tab3"]')); I think you missed * sign – Agnia Nov 25 '20 at 21:20
  • now i have received : SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*li[@id="tablist1-tab3"]' is not a valid XPath expression. – Test Nov 25 '20 at 21:25
  • in your new xpath tag element
  • is missing :D what I always do, I copy xpath from website and I put it into single quotes..
  • – Agnia Nov 25 '20 at 21:28
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="tablist1-tab3"]/span"} . I have copied the correct Xpath and i received the same error – Test Nov 25 '20 at 21:39
  • WebElement tab = driver.findElement(By.xpath("/html/body/div[3]/div[2]/div/div[2]/div[2]/div[1]/div/div/div/div[2]/div/div/div/div[2]/div/div/div/div/ul/li[3]")); tab.click(); – Test Nov 25 '20 at 21:43