Unable to locate element: {“method”:“xpath”,“selector”:“//li[@id=”tablist1-tab3“]”} error using Selenium and Java

為{幸葍}努か 提交于 2020-12-12 04:47:29

问题


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?


回答1:


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();
    



回答2:


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




回答3:


Make sure the element is not inside an iFrame. If it is, then you need to first find the iFrame element and use Selenium's switchTo(). After that you will be able to locate elements inside the iframe.



来源:https://stackoverflow.com/questions/65012531/unable-to-locate-element-methodxpath-selector-liid-tablist1-tab3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!