在Python Selenium中滚动到特定元素

2024-09-30 18:26:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图向下滚动到python中的一个paticle元素,但事实并非如此工作。任何建议改进!在

Loc_Hours =self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/div[5]/div[1]/div[1]/div[1]/div[2]/div[1]/p[1]")
self.driver.execute_script("return arguments[0].scrollIntoView();", Loc_Hours)
Loc_Hours.text()
self.driver.execute_script("window.scrollBy(0, -150);")

Tags: selfdiv元素executebydriverscriptelement
3条回答

你能试试这个吗:

from selenium.webdriver.common.action_chains import ActionChains

Loc_Hours = self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/div[5]/div[1]/div[1]/div[1]/div[2]/div[1]/p[1]")
ActionChains(self.driver).move_to_element(Loc_Hours)).perform()

我终于成功了!如果你有疑问就试试这个。在

print(Loc_Hours.get_attribute('textContent'))

这应该是有效的:

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

相关问题 更多 >