使用网页驱动程序滚动到元素?

我仍然在学习和回答我的一个问题: 给你,我被告知,这可能是因为在问题的元素不在视图中。

我浏览了文档,所以,这里有一个最相关的答案: 给你

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

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

当我尝试使用上面的方法滚动到元素时: 它说没有定义 WebElement。

我想这是因为我没有导入相关的模块。有人能告诉我该进口什么吗?

编辑: 正如 alexe 指出的,这是 Java 代码。

但同时,在我试图弄明白一段时间之后。我找到了 WebElement 的导入方法:

from selenium.webdriver.remote.webelement import WebElement

也许能帮到像我这样的人。

如何做到这一点也是一个很好的教训,国际海事组织:

转到: < a href = “ http://selenium-python.readthedocs.io/api.html # module-selenium.webdriver.Remote.webelement”rel = “ noReferrer”> Documentation 那个

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

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

193099 次浏览

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

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():

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

If you are interested in the differences:

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

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

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

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

Example

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

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

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

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

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

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)

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

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

Example:

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

This one always works for me for any type of selectors. There is also the Actions class, but for this case, it is not so reliable.