Selenium Python:如何滚动div

时间:2017-10-11 08:55:17

标签: python selenium-webdriver

我需要翻阅页面左侧列表中的多个页面here。要首先执行此操作,我需要向下滚动特定的div部分,然后单击next按钮。但滚动在一种情况下(url_ok)完美,而在另一种情况下不起作用(url_trouble)。没有任何想法为什么。 我正在测试的代码:

from selenium import webdriver
import time
driverPath = "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe"
driver = webdriver.Chrome(driverPath)   

url_trouble = 'https://2gis.ru/moscow/search/%D0%BF%D0%B0%D1%80%D0%BA%D0%BE%D0%B2%D0%BA%D0%B0%20/tab/geo?queryState=center%2F37.644653%2C55.827709%2Fzoom%2F12'   

url_ok = 'https://2gis.ru/moscow/search/%D0%BF%D0%B0%D1%80%D0%BA%D0%BE%D0%B2%D0%BA%D0%B0%20/tab/firms?queryState=center%2F37.644653%2C55.827805%2Fzoom%2F12'


def click(url, driver):
    driver.get(url)
    driver.maximize_window()
    time.sleep(5)
    next_link_data = driver.find_element_by_css_selector("div.pagination__arrow._right")
    next_link_data.location_once_scrolled_into_view
    next_link_data.click()

click(url_trouble, driver) # it doesn't work
click(url_ok, driver) # it works

所以问题是如何将url_trouble滚动到底部?

非常感谢您前进!

1 个答案:

答案 0 :(得分:0)

因为我猜两个" div.pagination__arrow._right"在页面中。

  

使用更具体的选择器
  尝试这个 - > " #module-1-13-1-1-2-2> div.pagination__arrow._right"   在

driver.find_element_by_css_selector("#module-1-13-1-1-2-2 > div.pagination__arrow._right")