gpt4 book ai didi

java - 无法在 selenium-java 中使用 xpath 错误定位元素

转载 作者:行者123 更新时间:2023-12-02 11:13:14 24 4
gpt4 key购买 nike

这是我要执行的代码

public WebDriver createPart() {

try {
driver.findElement(By.id("username")).sendKeys("502409373");
driver.findElement(By.id("password")).sendKeys("Magic14Magic");
driver.findElement(By.id("submitFrmShared")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
Select dropCountry = new Select(driver.findElement(By.id("txtNewLocation")));
dropCountry.selectByVisibleText("India");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[@class='btn']/label")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
Thread.sleep(10000);
driver.findElement(By.xpath("//li[@class='icon-button add']/span")).click();
driver.findElement(By.xpath("//div[@id='ENCActions']/a/label")).click();
driver.findElement(By.xpath("//label[starts-with(text(),'Create Part...')]")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = driver.findElement(By.xpath("//select[@id='Type-Field']//following-sibling::div//div//input"));
element.click();
Thread.sleep(3000);
element.sendKeys(Keys.BACK_SPACE);
Thread.sleep(3000);
element.sendKeys("Subassy");
Thread.sleep(4000);
driver.findElement(By.xpath("//div[@data-value='Subassy']")).click();
driver.findElement(By.xpath("//span[text()='Description']//parent::td//following-sibling::td//textarea")).sendKeys("Testing");
driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']//parent::select")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']")).click();
driver.findElement(By.xpath("//a[text()='Done']")).click();
driver.switchTo().window(parentWindowHandler);
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 60);
element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Part Details']")));
element.click();

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return driver;

}

这是该元素的html代码,我找不到。

<div class="tabcontent">
<a class="tablink" href="javascript:void(0);" title="Part Details"
onclick="tvcTabPage_tabClicked(this,true)">Part Details</a>
<a href="javascript:void(0);" class="closetab"
onclick="tvcTabPage_tabClosed(this)" title="Close Tab"></a>
</div>

这是堆栈跟踪

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected 
condition failed: waiting for element to be clickable: By.xpath:
//a[@title='Part Details'] (tried for 60 second(s) with 500 MILLISECONDS
interval)

Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an
element using By.xpath: //a[@title='Part Details']

尽管我已经使用了等待语句。我无法找到该元素。我已经使用 chrome xpath 验证了 xpath。请帮助我解决我的问题。 Selenium 版本:3.3.1。实际流程是,我单击主页中的链接,结果是一个包含表单的弹出窗口。填写表格并单击“完成”后,弹出窗口会自动关闭,主页会刷新并打开一个新页面。现在,我尝试单击新主页中的“零件详细信息”链接,但该链接不起作用。

我添加了这段代码来查找正确的窗口处理程序。

subWindowHandler = null;
Set<String> handles1 = driver.getWindowHandles(); // get all window handles
iterator = handles1.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@title='Part Details']")));
System.out.println("KK1");
}

给我输出:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected 
condition failed: waiting for visibility of element located by By.xpath:
//a[@title='Part Details'] (tried for 10 second(s) with 500 MILLISECONDS
interval)

这里,所需的“零件详细信息”元素位于我已包含在代码中的框架中。但是,我无法在框架内找到该元素。这是我修改后的代码。

System.out.println(driver.switchTo().window(parentWindowHandler).getTitle());           
driver.switchTo().frame("content");
driver.switchTo().frame("detailsDisplay");
driver.findElement(By.xpath("//a[@title='Part Details']")).click();

但是我遇到了这个错误。

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no 
such element: Unable to locate element:
{"method":"xpath","selector":"//a[@title='Part Details']"}

我附上了 html 代码的屏幕截图。 HTML screenshot

最佳答案

更改 visibilityOfElementLocated 而不是 elementToBeClickable。你可以直接找到webeelement,然后点击它,如下图:

element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@title='Part Details']")));

关于java - 无法在 selenium-java 中使用 xpath 错误定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462658/

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