gpt4 book ai didi

java - 即使使用 JavascriptExecutor 滚动也无法点击元素

转载 作者:行者123 更新时间:2023-11-29 04:26:09 27 4
gpt4 key购买 nike

下面是我的测试脚本。即使我正在滚动窗口,我也无法单击该元素。我也尝试使用显式等待。我收到 WebDriverException 消息说 Element is not clickable at point (588, 1611)

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
int success = 0;
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
driver.findElement(By.xpath("//a[text()='Categories/Products']")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0,3004)","");

String str = "Mockingjay";
List <WebElement> lt1 = driver.findElements(By.tagName("td"));
Iterator <WebElement> it1 = lt1.iterator();

while(it1.hasNext())
{

WebElement el = it1.next();
if(el.getText().contains(str))
{
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(el));
el.click();
break;
}

}

driver.findElement(By.xpath("//span[text()='Edit']")).click();

if(driver.findElement(By.xpath("//td[text()='Products Status:']/following-sibling::td/input[1]")).isSelected())
{
System.out.println("Changing the product status to 'Out of Stock'");
}

这是源代码。我尝试将所有 td 标签 放入列表中,在它们之间迭代以找到 元素 并单击它。

 <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1391'">
<td class="dataTableContent"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1391&amp;action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="Preview" title=" Preview " /></a> LED TV/52"</td>
<td class="dataTableContent" align="center">
<img src="images/icon_status_green.gif" border="0" alt="Active" title=" Active " width="10" height="10" />  <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&amp;flag=0&amp;pID=1391&amp;cPath="><img src="images/icon_status_red_light.gif" border="0" alt="Set Inactive" title=" Set Inactive " width="10" height="10" /></a></td>
<td class="dataTableContent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1391"><img src="images/icon_info.gif" border="0" alt="Info" title=" Info " /></a> </td>
</tr>
<tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1494'">
<td class="dataTableContent"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1494&amp;action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="Preview" title=" Preview " /></a> Mock</td>
<td class="dataTableContent" align="center">
<img src="images/icon_status_green.gif" border="0" alt="Active" title=" Active " width="10" height="10" />  <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&amp;flag=0&amp;pID=1494&amp;cPath="><img src="images/icon_status_red_light.gif" border="0" alt="Set Inactive" title=" Set Inactive " width="10" height="10" /></a></td>
<td class="dataTableContent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1494"><img src="images/icon_info.gif" border="0" alt="Info" title=" Info " /></a> </td>
</tr>
<tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1507'">
<td class="dataTableContent"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1507&amp;action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="Preview" title=" Preview " /></a> Mockingjay</td>
<td class="dataTableContent" align="center">
<img src="images/icon_status_green.gif" border="0" alt="Active" title=" Active " width="10" height="10" />  <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&amp;flag=0&amp;pID=1507&amp;cPath="><img src="images/icon_status_red_light.gif" border="0" alt="Set Inactive" title=" Set Inactive " width="10" height="10" /></a></td>
<td class="dataTableContent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cPath=&amp;pID=1507"><img src="images/icon_info.gif" border="0" alt="Info" title=" Info " /></a> </td>
</tr>

最佳答案

当我写这样的代码时,我会尽量让事情变得简单。我将要重用的代码放入函数中,以便它们更易于调用和维护。

在您的情况下,您正在遍历所有 TD 以寻找包含与您要查找的产品匹配的文本的元素。我们可以使用 XPath 搜索特定元素,而不是这样做。

//td[@class='dataTableContent'][contains(.,'Mockingjay')]

您还有用于滚动、等待元素可点击等的代码,但这些都不是必需的。在 Java/Selenium 中,当您尝试单击某个元素时,如果它不可见,页面将自动滚动。

我查看了您的代码并将其分解为不同的操作并为每个操作编写了一个函数。然后我编写了代码,使用这些函数来完成您想要做的事情。这是工作代码,我没有看到 Element is not clickable 错误。

函数如下

public static void deleteSelectedCategory()
{
driver.findElement(By.cssSelector("#tdb4 > span.ui-button-text")).click();
}

public static void editSelectedCategory()
{
driver.findElement(By.cssSelector("#tdb3 > span.ui-button-text")).click();
}

public static boolean isProductInStock(String productName)
{
return !driver.findElements(By.xpath("//tr[./td[@class='dataTableContent'][contains(.,'" + productName + "')]]//img[@src='images/icon_status_green.gif']")).isEmpty();
}

public static void login(String username, String password)
{
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("tdb1")).click();
}

public static void selectCategory(String categoryName)
{
driver.findElement(By.xpath("//a[.='Categories/Products']")).click();
driver.findElement(By.xpath("//td[@class='dataTableContent'][contains(.,'" + categoryName + "')]")).click();
}

这是驱动您的场景的代码

String productName = "Mockingjay"; // in stock
// String productName = "Baloon"; // out of stock
driver.get("http://www.gcrit.com/build3/admin");
login("admin", "admin@123");

selectCategory(productName);
boolean inStock = isProductInStock(productName);
System.out.println("Product is in stock: " + inStock);
if (inStock)
{
editSelectedCategory();
System.out.println("Changing the product status to 'Out of Stock'");
// set the product to out of stock
}

顺便说一句,您无需编辑产品即可查看其是否缺货。红灯/绿灯表示是否缺货。我编写了代码来检查页面上是否显示绿灯,如果是,然后编辑产品并添加一个占位符,您可以在其中添加代码以将产品更改为缺货等。

关于java - 即使使用 JavascriptExecutor 滚动也无法点击元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46143300/

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