作业不滚动到网页上的特定文本

时间:2017-10-27 10:06:12

标签: python python-3.x selenium scroll web-scraping

我的工作完全有效,除了滚动到此元素://span[text()="Next Page"]element = driver.find_element_by_xpath('//span[text()="Next Page"]') driver.execute_script("return arguments[0].scrollIntoView();", element)

当我手动滚动时,它会起作用。

我试图解决滚动到这个文本的问题:

clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, ('//span[text()="Next Page"]'))))
clickMe2 = driver.find_elements_by_xpath('//span[text()="Next Page"]')
actions = ActionChains(driver)
actions.move_to_element(clickMe2).click()
element = driver.find_element_by_xpath('//span[text()="Next Page"]')
element.location_once_scrolled_into_view
driver.execute_script("arguments[0].scrollIntoView();", clickMe2)

以及:

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

它完成了这项工作,然后在以下行中发出错误:Traceback (most recent call last): File "C:/Users/Bain3/PycharmProjects/untitled4/Crownbet.py", line 2674, in <module> driver.execute_script("arguments[0].scrollIntoView();", clickMe2) File "C:\Users\Bain3\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 532, in execute_script 'args': converted_args})['value'] File "C:\Users\Bain3\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute self.error_handler.check_response(response) File "C:\Users\Bain3\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: arguments[0].scrollIntoView is not a function (Session info: chrome=61.0.3163.100) (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64)

完整错误:

#SCROLL_PAUSE_TIME = 0.5


#last_height = driver.execute_script("return document.body.scrollHeight")

#while True:

    #driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")


    #time.sleep(SCROLL_PAUSE_TIME)


    #new_height = driver.execute_script("return document.body.scrollHeight")
    #if new_height == last_height:
        #break
    #last_height = new_height

完整page

已更新

我之前已将其删除:

clickMe = wait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, ('//span[text()="Next Page"]'))))
element1 = driver.find_element_by_xpath('//span[text()="Next Page"]')
driver.execute_script("return arguments[0].scrollIntoView();", element1)

但是,它似乎滚动整个页面而不是该特定元素。这是:

{{1}}

2 个答案:

答案 0 :(得分:1)

实际上你正在向右滚动到所需的元素,但是有一个导航面板(带有搜索栏,登录和立即加入按钮),它固定在顶部并覆盖了下一个按钮。您只需向上滚动一点即可看到下一个按钮。请尝试以下代码:

from selenium.webdriver.common.keys import Keys

header = driver.find_element_by_tag_name('header')
driver.execute_script('arguments[0].hidden="true";', header)
element = driver.find_element_by_xpath('//span[text()="Next Page"]')
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.find_element_by_tag_name('body').send_keys(Keys.UP)
element.click()

答案 1 :(得分:0)

请参考以下代码: -

targetElement = driver.findElement(By.xpath("your xpath"));
JavascriptExecutor js = ((JavascriptExecutor) driver);
// This:
js.executeScript("arguments[0].scrollIntoView(true);", targetElement);
targetElement.click();
// Or maybe even just:
js.executeScript("arguments[0].click();", targetElement);