代码之家  ›  专栏  ›  技术社区  ›  koushick

在python selenium中滚动到特定元素

  •  1
  • koushick  · 技术社区  · 7 年前

    我试图向下滚动到python中的特定元素,但它不起作用。有任何改进建议!

    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);")
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Nihal Saranga    7 年前

    你能试试这个吗

    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()
    
        2
  •  0
  •   koushick    7 年前

    这对我来说是成功的!如果你有疑问就试试这个。

    print(Loc_Hours.get_attribute('textContent'))
    
        3
  •  0
  •   Alichino    7 年前

    这应该起作用:

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