简体   繁体   English

如何使用 Selenium WebDriver 检查元素是否进入视图?

[英]How to check if an element is into view using Selenium WebDriver?

I have to check if an element is in view and if not, only then scroll it into view using following and then interact with it -我必须检查一个元素是否在视图中,如果没有,只有使用以下方法将其滚动到视图中,然后与它交互 -

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);

If an element is in view, don't execute above command.如果一个元素在视图中,不要执行上面的命令。 To check if an element is in view, isDisplayed() is not helping because it returns true irrespective of an element is in view or not.要检查元素是否在视图中, isDisplayed()没有帮助,因为无论元素是否在视图中,它都会返回 true。

Can you please suggest what will help to achieve to check if an element is in view?您能否建议什么有助于实现检查元素是否在视图中?

To interact with an element the element needs to be within the Viewport .要与元素交互,该元素需要位于Viewport 内 If the element is not within the Viewport you have to scroll() as follows:如果元素不在视口内,则必须按如下方式scroll()

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);

You can find a detailed discussion on different scrolling option in What is the difference between the different scroll options?您可以在不同滚动选项之间的区别是什么?中找到有关不同滚动选项的详细讨论


At this point it is worth to mention, the following methods:此时值得一提的是,有以下方法:

will automatically scroll the element within the Viewport before getting executed.将在执行之前自动滚动视口内的元素。

You can find a detailed discussion in Selenium python Error: element could not be scrolled into view您可以在Selenium python Error: element could not be scrolled into view 中找到详细的讨论


Finally, in the rarest of the rare cases if the HTML DOM comprises of Angular or React elements you may even have to induce WebDriverWait for the visibilityOfElementLocated() before you attempt to scroll as follows:最后,在极少数情况下,如果HTML DOM包含AngularReact元素,您甚至可能必须在尝试滚动之前引入WebDriverWait以获取visibilityOfElementLocated() ,如下所示:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.nsg-button"))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果站点使用Ajax,如何检查Selenium WebDriver? - How to check with Selenium WebDriver if a site is using Ajax? 如何使用 JavaScript 单击 Selenium WebDriver 中的元素? - How to click an element in Selenium WebDriver using JavaScript? 如何与使用Selenium Webdriver隐藏的元素进行交互? - How to interact with an element that is hidden using Selenium Webdriver? JavaScript-Selenium Webdriver。 如何检查元素是否可见 - JavaScript - Selenium Webdriver. How to check if element is visible 如何使用selenium webdriver找到伪元素的父母? - How do I find parents of pseudo element using selenium webdriver? 如何使用Selenium WebDriver单击列表中的特定元素? - How to click on particular element in a list using selenium webdriver? Selenium-WebDriver 如何使用 javascript 和 firefox 浏览器突出显示元素 - Selenium-WebDriver how to highlight element using javascript and firefox browser 如何使用Safari的JavaScript双击Selenium Webdriver上的元素 - How to double click an element on Selenium Webdriver using JavaScript for Safari 如何使用 Javascript 选择 Selenium Webdriver 中的最后一个元素? - How to select last element in Selenium Webdriver using Javascript? 如何使用 Selenium WebDriver 和 Java 从元素中检索文本 - How to retrieve text from an element using Selenium WebDriver and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM