gpt4 book ai didi

java - Selenium:由于线程错误,无法滚动到单击不可见的元素:元素不可见

转载 作者:行者123 更新时间:2023-12-02 01:47:31 25 4
gpt4 key购买 nike

我正在尝试单击一个不可见且需要向下滚动才能可见的元素。为了解决这个问题,我尝试使用 javascript 执行器和操作,但它们不起作用,因为在滚动之前,我收到一条线程错误,指出该元素不可见。我已确保元素的 xpath 正确,并验证代码适用于无需滚动即 cocoa 见的元素。

<div class="product-grid-item clearfix" data-alpha="LOG ON T-SHIRT BLACK" data-price="4800" data-i="27">

<a href="/products/8r9ya45zmdwz" class="product-link">

<img src="[//cdn.shopify.com/s/files/1/0923/4190/products/Palace-2019-Autumn-T-Shirt-Log-On-black-1336\_200x200\_crop\_center@2x.jpg?v=1565334138](//cdn.shopify.com/s/files/1/0923/4190/products/Palace-2019-Autumn-T-Shirt-Log-On-black-1336_200x200_crop_center@2x.jpg?v=1565334138)" alt="LOG ON T-SHIRT BLACK" class="img">

</a>

<div class="product-info">

<a href="/products/8r9ya45zmdwz" class="product-link">

<h3 class="title">LOG ON T-SHIRT BLACK</h3>

</a>

<div class="price">

<span class="prod-price">$48</span>
</div>

</div>

</div>

我尝试过 javascript 执行器和操作

WebElement element = driver.findElement(By.xpath("//*[@data-alpha='" + productName + "' and @class='product-grid-item clearfix']")); //error occurs at this line

int elementPosition = element.getLocation().getY();
String js = String.format("window.scroll(0, %s)", elementPosition);
((JavascriptExecutor)driver).executeScript(js);
element.click();

WebElement element = driver.findElement(By.xpath("//*[@data-alpha='" + productName + "' and @class='product-grid-item clearfix']")); //error occurs at this line
Actions builder = new Actions(driver);
builder.moveToElement(element);
builder.click();
builder.build().perform();

错误消息:

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@data-alpha='WINDOWLICKER HOOD GREY MARL' and @class='product-grid-item clearfix']"}

最佳答案

尝试使用WebDriverWait并使用contains更改定位器,可能包含空格。

WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(@data-alpha,'" + productName.trim() + "') and @class='product-grid-item clearfix']")));

scroll here....

关于java - Selenium:由于线程错误,无法滚动到单击不可见的元素:元素不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57446810/

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