Python 使用 webdriver 滚动到元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41744368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:32:57  来源:igfitidea点击:

Scrolling to element using webdriver?

pythonpython-3.xseleniumselenium-webdriver

提问by Sid

I am still learning and in response to one of my questions: here, I was told to that it might be due because the element in question is not in view.

我仍在学习并回答我的一个问题:在这里,我被告知这可能是由于所讨论的元素不在视野中。

I looked through the documentation and SO, here was the most relevant answer: here

我查看了文档和 SO,这是最相关的答案:here

You can use the "org.openqa.selenium.interactions.Actions" class to move to an element:

您可以使用“org.openqa.selenium.interactions.Actions”类移动到一个元素:

WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
## actions.click();
actions.perform();

When I try to use the above to scroll to the element: It says WebElement not defined.

当我尝试使用上述内容滚动到元素时:它说未定义 WebElement。

I think this is because I have not imported the relevant module. Can someone point out what I am supposed to import?

我想这是因为我没有导入相关模块。有人可以指出我应该导入什么吗?

Edit: As pointed out by alecxe, this was java code.

编辑:正如 alecxe 所指出的,这是 java 代码。

But in the meantime right after trying to figure it out for some time. I have found out the import method for WebElement:

但与此同时,在试图弄清楚一段时间之后。我已经找到了 WebElement 的导入方法:

from selenium.webdriver.remote.webelement import WebElement

Might help someone like me.

可能会帮助像我这样的人。

The how of it is also a good lesson, IMO:

它的方法也是一个很好的教训,IMO:

Went to: DocumentationThe

去:文档

class selenium.webdriver.remote.webelement.WebElement(parent, id_, w3c=False)

Need to be separated into the command form mentioned above.

需要拆分成上面提到的命令形式。

回答by alecxe

You are trying to run Java code with Python. In Python/Selenium, the org.openqa.selenium.interactions.Actionsare reflected in ActionChainsclass:

您正在尝试使用 Python 运行 Java 代码。在 Python/Selenium 中,org.openqa.selenium.interactions.Actions反映在ActionChainsclass 中

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("my-id")

actions = ActionChains(driver)
actions.move_to_element(element).perform()

Or, you can also "scroll into view" via scrollIntoView():

或者,您也可以通过以下方式“滚动查看” scrollIntoView()

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

If you are interested in the differences:

如果您对差异感兴趣:

回答by Andersson

It's not a direct answer on question (its not about Actions), but it also allow you to scroll easily to required element:

这不是问题的直接答案(不是关于Actions),但它也允许您轻松滚动到所需元素:

element = driver.find_element_by_id('some_id')
element.location_once_scrolled_into_view

This actually intend to return you coordinates (x, y) of element on page, but also scroll down right to target element

这实际上打算返回页面上元素的坐标 ( x, y),但也会向右向下滚动到目标元素

回答by Neil C. Obremski

In addition to move_to_element()and scrollIntoView()I wanted to pose the following code which attempts to centerthe element in the view:

除了move_to_element()scrollIntoView()我想提出以下的代码的企图居中的元件在视图中:

desired_y = (element.size['height'] / 2) + element.location['y']
window_h = driver.execute_script('return window.innerHeight')
window_y = driver.execute_script('return window.pageYOffset')
current_y = (window_h / 2) + window_y
scroll_y_by = desired_y - current_y

driver.execute_script("window.scrollBy(0, arguments[0]);", scroll_y_by)

回答by ePandit

This can be done using driver.execute_script():-

这可以使用 driver.execute_script() 来完成:-

driver.execute_script("document.getElementById('myelementid').scrollIntoView();")

回答by Andersson

There is another option to scroll page to required element if element has "id"attribute

如果元素具有属性,还有另一个选项可以将页面滚动到所需元素"id"

If you want to navigate to page and scroll down to element with @id, it can be done automatically by adding #element_idto URL...

如果要导航到页面并使用 向下滚动到元素@id,可以通过添加#element_id到 URL来自动完成...

Example

例子

Let's say we need to navigate to Selenium Waits documentation and scroll page down to "Implicit Wait" section. We can do

假设我们需要导航到 Selenium Waits 文档并将页面向下滚动到“隐式等待”部分。我们可以做的

driver.get('https://selenium-python.readthedocs.io/waits.html')

and add code for scrolling...OR use

并添加滚动代码...或使用

driver.get('https://selenium-python.readthedocs.io/waits.html#implicit-waits')

to navigate to page AND scroll page automatically to element with id="implicit-waits"(<div class="section" id="implicit-waits">...</div>)

导航到页面并自动滚动页面到带有id="implicit-waits"( <div class="section" id="implicit-waits">...</div>) 的元素

回答by Antony Fuentes Artavia

You can scroll to the element by using javascript through the execute_javascriptmethod. For example here is how I do it using SeleniumLibrary on Robot Framework:

您可以通过该execute_javascript方法使用 javascript 滚动到该元素。例如,这是我在 Robot Framework 上使用 SeleniumLibrary 的方法:

web_element = self.selib.find_element(locator)
self.selib.execute_javascript(
    "ARGUMENTS",
    web_element,
    "JAVASCRIPT",
    'arguments[0].scrollIntoView({behavior: "instant", block: "start", inline: "start"});'
)