您可以使用“ 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();
当我尝试使用以上内容滚动到元素时:它表示未定义WebElement。
我认为这是因为我尚未导入相关模块。有人可以指出我应该导入的内容吗?
编辑:正如alecxe指出的,这是Java代码。
但是与此同时,在试图弄清楚一段时间之后。我发现了WebElement的导入方法:
from selenium.webdriver.remote.webelement import WebElement
可能会帮助像我这样的人。
IMO也是一个很好的教训:
去:文档 的
class selenium.webdriver.remote.webelement.WebElement(parent, id_, w3c=False)
需要分成上面提到的命令形式。
您正在尝试使用Python运行Java代码。在Python /
Selenium中,org.openqa.selenium.interactions.Actions
反映在ActionChains
类中:
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()
或者,您也可以通过以下方式“滚动查看” scrollIntoView()
:
driver.execute_script("arguments[0].scrollIntoView();", element)