Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 硒:Can';t滚动以单击由于线程错误而不可见的元素:元素不可见_Java_Selenium_Selenium Webdriver_Xpath_Webdriverwait - Fatal编程技术网

Java 硒:Can';t滚动以单击由于线程错误而不可见的元素:元素不可见

Java 硒:Can';t滚动以单击由于线程错误而不可见的元素:元素不可见,java,selenium,selenium-webdriver,xpath,webdriverwait,Java,Selenium,Selenium Webdriver,Xpath,Webdriverwait,我试图单击一个不可见的元素,需要向下滚动才能看到它。为了解决这个问题,我尝试使用javascript executor和action,但它们不起作用,因为在滚动之前,我收到一个线程错误,说元素不可见。我已经确保元素的xpath是正确的,并验证了代码是否可以使用无需滚动即可看到的元素 <div class="product-grid-item clearfix" data-alpha="LOG ON T-SHIRT BLACK" data-price="4800" data-i="27"&g

我试图单击一个不可见的元素,需要向下滚动才能看到它。为了解决这个问题,我尝试使用javascript executor和action,但它们不起作用,因为在滚动之前,我收到一个线程错误,说元素不可见。我已经确保元素的xpath是正确的,并验证了代码是否可以使用无需滚动即可看到的元素

<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>

错误消息:

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....
该元素似乎是一个动态元素,正如您所提到的,该元素不可见,需要向下滚动才能可见,以便您可以使用以下任一解决方案:

  • 使用WebDriverWait和ExpectedConditions以及
    ElementToBickable()

  • 通过操作使用WebDriverWait和ExpectedConditions以及
    ElementToBickable()


您发布的错误消息没有说明该元素不可见,而是说明找不到该元素。你确定你的定位器是正确的吗?您是否检查过元素是否在IFRAME中?你试过添加等待吗?@JeffC我试过所有这些方法,我非常确定我的定位器是正确的,因为当我手动向下滚动到元素时,它会工作。如果可能,请尝试提供HTML屏幕截图的完整代码。更好的做法是理解代码并尝试找到解决方案。除此之外,尝试使用一些jquerys方法按百分比滚动页面,或者可以滚动一次整页来验证这些内容。@Vishal我认为我没有滚动过它。错误发生在driver.findElement()上,因此如果我使用wait.until(),它甚至不会滚动,它是否会一直等待,因此无法滚动?请使用
WebDriverWait
确保元素存在,然后执行滚动。
no such element: Unable to locate element: {"method":"xpath","selector":"//*[@data-alpha='WINDOWLICKER HOOD GREY MARL' and @class='product-grid-item clearfix']"}
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....
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='product-grid-item clearfix' and contains(@data-alpha, 'LOG ON T-SHIRT BLACK')]//a[@class='product-link' and contains(@href, 'products')]/img[contains(@src, 'shopify')]"))).click();
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='product-grid-item clearfix' and contains(@data-alpha, 'LOG ON T-SHIRT BLACK')]//a[@class='product-link' and contains(@href, 'products')]/img[contains(@src, 'shopify')]")))).click().build().perform();