Java Xpath不能与selenium Web驱动程序一起使用
我在识别以下网页中的xpath时遇到问题 这是我想要识别的元素: 这是我正在使用的xpath:Java Xpath不能与selenium Web驱动程序一起使用,java,selenium,xpath,Java,Selenium,Xpath,我在识别以下网页中的xpath时遇到问题 这是我想要识别的元素: 这是我正在使用的xpath:/html/body/div/div/div[1]/h1/a 这是我正在使用的代码: public WebElement Empresa (WebDriver driver, int Iterator) { //WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visi
/html/body/div/div/div[1]/h1/a
这是我正在使用的代码:
public WebElement Empresa (WebDriver driver, int Iterator) {
//WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/div[1]/h1/a")));
return driver.findElement(By.xpath("/html/body/div/div/div[1]/h1/a"));
}
最后,这是错误日志:
我尝试了driver.findElement(通过.xpath(“/html/body/div/div/div[1]/h1/a”)代码>但也不起作用
你能帮我一下吗?我已经更新了下面的方法。
请用这个
public WebElement Empresa (WebDriver driver, int Iterator) {
//WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
WebElement elem = driver.findElement(By.className("lb-leaderboard-widget-wrapper"));
WebElement elemH1 = elem.findElement(By.tagName("h1"));
WebElement elemIWant = elem.findElement(By.tagName("a"));
System.out.println(elemIWant.getAttribute("innerHTML").toString());
return elemIWant;
}
让我知道您的反馈。它在iframe中。首先切换到帧并尝试识别它
public WebElement Empresa (WebDriver driver, int Iterator) {
driver.switchTo().frame(0);
String xpath="/html/body/div/div/div[1]/h1/a";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
return driver.findElement(By.xpath(xpath));
}
如果查看HTMLDOM
,WebElement
位于
中。因此,我们需要首先使用适当的WebDriverWait
切换到
,然后按如下方式定位WebElement
:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));
return driver.findElement(By.xpath("div[@class='lb-leaderboard-header']/h1/a[@class='lb-leaderboard-name']"));
它位于iframe中。首先切换到框架,并尝试下面给出的答案。我已经添加了一个答案。请尝试使用它,让我知道你的反馈效果很好!非常感谢。一些问题:1。-看起来代码的运行速度比网页的加载速度快,有没有简单的方法来等待显示元素的同步?2.-你怎么知道这是在一个框架中,框架(编号)是什么?3.-如何切换回页面?感谢1.webdriver等待是等待的最佳方式。2.如果父标记中的任何一个是iframe。你可以说,它在iframe里面。3.您可以使用driver.switchTo().defaultContent()返回主html。@Ivan Garrido您能检查我的答案并告诉我您的反馈吗。