gpt4 book ai didi

selenium - Selenium xpath即使在firepath中也可以工作,但没有此类元素异常

转载 作者:行者123 更新时间:2023-12-03 16:59:35 27 4
gpt4 key购买 nike

这是我使用Firebug检查元素时的样子

enter image description here

当我在xpath中尝试相同的语法时,它选择了结果页面2。我在selenium IDE中尝试了相同的方法,然后单击“查找”,但是它在执行代码时选择了结果页面2。我正在获取无此类元素异常

Xpath语法://a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']

public void jobSearch(){
WebDriver driver= new FirefoxDriver();
driver.get("https://www.indeed.com");
driver.findElement(By.id("what")).sendKeys("QA Engineer");
driver.findElement(By.id("where")).clear();
driver.findElement(By.id("where")).sendKeys("Seattle,WA");
driver.findElement(By.id("fj")).click();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

driver.findElement(By.xpath("//a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']")).click();


感谢您的宝贵时间和宝贵的建议。

最佳答案

实际上有3个错误:


最大的错误是,Script无法找到下一页的可见选项。
在给定的屏幕截图中,运行给定代码时的结果。这就是脚本无法找到元素的原因。


Selenium Webdriver脚本仅适用于可见区域

enter image description here

解决方案:

添加向下滚动网页的步骤

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,1000)", "");



Xpath不能一概而论。无论将URL放置在 2 num页上,它都是用于获取。因此更改如下:

//div[@class='pagination']//span[text()='2']
有一些中断,例如弹出窗口,基于区域的URL。因此,编写代码就像下面给出的,以消除将来的错误:

public void jobSearch()   {


try{
WebDriver driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.indeed.com");
try
{
//Here region based URL gets open so remove it if it is directly open www.indeed.com
driver.findElement(By.linkText("www.indeed.com")).click();
}
catch (Exception e)
{

}
driver.findElement(By.id("what")).sendKeys("QA Engineer");
driver.findElement(By.id("where")).clear();
driver.findElement(By.id("where")).sendKeys("Seattle,WA");
driver.findElement(By.id("fj")).click();
try
{
// After this one pop up gets open so close it
driver.findElement(By.xpath("//button[@id='prime-popover-close-button']/span")).click();
}
catch (Exception e)
{

}
//pageDown(driver.findElement(By.id("searchCount")), 2);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,1000)", "");
//driver.findElement(By.xpath("//a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']")).click();
driver.findElement(By.xpath("//div[@class='pagination']//span[text()='2']")).click();
// Now continue you code here
}
catch (Exception e)
{
e.printStackTrace();
} }



注意:对于具有Selenium 3.0+版本的Firefox,请阅读 GeckoDriver,对于Chrome浏览器,请阅读 Chromedriver

关于selenium - Selenium xpath即使在firepath中也可以工作,但没有此类元素异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41094019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com