Selenium 找不到加载项文本的xpath元素(正确的xpath)
无法找到xpathSelenium 找不到加载项文本的xpath元素(正确的xpath),selenium,xpath,Selenium,Xpath,无法找到xpath//a[contains(text(),'Add-Ons')的元素。 这是关于帧的吗?当我手动验证时,您的xpath可以找到正确的元素。我怀疑页面没有完全加载,请在dirver.get()之后添加一些sleep,以便进行调试 driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(30,TimeUnit.S
//a[contains(text(),'Add-Ons')的元素。
这是关于帧的吗?当我手动验证时,您的xpath可以找到正确的元素。我怀疑页面没有完全加载,请在
dirver.get()之后添加一些sleep
,以便进行调试
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get("http://www.spicejet.com");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
.build().perform();
Thread.sleep(3000);
driver.findElement(By.linkText("Hot Meals ")).click();
driver.close();
有时,使用By.linkText()
您只需使用css选择器
,如下所示:
driver.get("http://www.spicejet.com");
Thread.sleep(10*1000) // sleep 10 seconds to wait page open completely.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
.build().perform();
driver.findElement(By.cssSelector("#header-addons > ul > li:nth-child(5) > a")).click();
Thread.sleep(3000);
代替行driver.findElement(By.linkText(“热餐”))。单击();
您还可以使用xpath
,如下所示:
driver.get("http://www.spicejet.com");
Thread.sleep(10*1000) // sleep 10 seconds to wait page open completely.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
.build().perform();
driver.findElement(By.cssSelector("#header-addons > ul > li:nth-child(5) > a")).click();
Thread.sleep(3000);
添加HTML代码以供参考,它位于何处以及如何定位。似乎所需的下拉菜单仅在全屏模式下显示,因此要使代码正常工作,请检查是否使用driver.manage().window().maximize()
@Chinmay我在我的电脑中使用chrome运行代码。如果您使用cssSelector
而不是linkText
,您的代码几乎正确且运行良好。如果您在driver.get()之后使用大约5/6秒的睡眠时间,这将非常好
method.@chin如果答案真的对您有帮助,请接受。有时使用.linkText()找不到确切的元素……您能解释一下“有时”吗?如果它总是有效的话!还要注意的是,By.xpath(//a[contains(text(),'Add-Ons')])
,而不是By.linkText(“热餐”)中的问题
,因此您的解决方案是useless@Andersson我成功地运行了这个。你想得到一个视频屏幕吗?我不会在没有保证的情况下发布任何答案。你怎么说这个解决方案对于否决它是无用的?