gpt4 book ai didi

java - 有时 webdriver 会找到该元素,但有时对于相同的 xpath,它不会给出此类元素异常

转载 作者:行者123 更新时间:2023-12-01 12:48:57 26 4
gpt4 key购买 nike

我创建了一个测试脚本,我想在其中单击编辑按钮,然后关闭窗口。大多数时候,代码按预期工作,但有时对于相同的代码,我会遇到 nosuchelement 异常。我浏览了论坛中提出的类似问题,但没有一个解决方案对我有用。下面我将 java 代码与 HTML 代码一起放置,希望能找到解决方案。

for(String newwindow : window.getWindowHandles()){
//swithching to the new pop up using window.switchTo().window(passing newwindow as argument)
window.switchTo().window(newwindow);}
//getting title of new window using getTitle() method
System.out.println("NewWindow Title"+ window.getTitle());
window.findElement(By.xpath(".//*[@id='edit_resume_section3_open' and not(@disabled)]")).click();
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
System.out.println(window.findElement(By.xpath("//input[@name='title']")).getAttribute("value"));
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
window.findElement(By.id("update")).click();
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
window.close();

Html Code for the webelement-
<head>
<body class="bg_lightgreen" marginwidth="0" marginheight="0" onload="" leftmargin="0" topmargin="0" style="">
<iframe id="_yuiResizeMonitor" style="position: absolute; visibility: visible; width: 10em; height: 10em; top: -120px; left: -120px; border-width: 0px;"/>
<div id="fade_nation_mismatch" class="black_overlay"/>
<div id="fade_visual_resume" class="black_overlay"/>
<div id="show_visual_resume" class="white_content" style="position: absolute; top: 25%;">
<table class="bg_purple" width="100%" cellspacing="0" cellpadding="0" border="0">
<table class="bg_white" width="998" cellspacing="0" cellpadding="20" border="0" align="center">
<table class="bg_white" width="998" cellspacing="0" cellpadding="20" border="0" align="center">
<tbody>
<tr>
<td class="font_15 txt_purple bold" width="203" style="padding-bottom:5px; width:250px; word-wrap: break-word;">1.6YR Experiance/BSC(CS)</td>
<td width="715" style="padding-bottom:5px;">
<a id="edit_resume_section3_open" class="thickbox" title="Professional Details" style="cursor:pointer" onclick="showEditSection(3);">[Edit]</a>
</td>
</tr>

最佳答案

首先确保您的 xpath 表达式(或用于查找元素的任何表达式)在任何情况下都是正确的。对于动态生成的元素,您的 xpath 表达式在任何情况下都无效。

如果正确,则意味着您遇到了竞争条件。您正在寻找的元素有时不存在。这可能取决于您的浏览器当时可能需要更多时间来呈现页面,或者取决于 http 服务器可能需要更多时间来为您的页面提供服务。可能有很多(愚蠢的)原因。

为了解决这个问题,你必须使用池化。检查您的元素是否存在。如果存在,则继续。如果不存在,请等待并重试。类“com.thoughtworks.selenium.Wait”适合这个目的。

您将在 Google 中找到使用“com.thoughtworks.selenium.Wait example”作为关键字的示例。

关于java - 有时 webdriver 会找到该元素,但有时对于相同的 xpath,它不会给出此类元素异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405666/

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