简体   繁体   English

无法在python中使用selenium在网页中加载“显示更多”?

[英]Can't load “Show more” in a webpage using selenium in python?

I am trying to load "Show More" using selenium in python. 我想在python中使用selenium加载“Show More”。 However, the code is running and executing fine but I am getting 'Message' and 'complete'(this I understand) in python console. 但是,代码正在运行并且执行正常,但我在python控制台中收到'Message'和'complete'(我理解)。 Also, the webpage I am trying to open is opening but as soon as I try to scroll down it is showing a window, "This Safari window is remotely controlled by an automated test." 此外,我试图打开的网页正在打开,但是当我尝试向下滚动时,它会显示一个窗口,“此Safari窗口由自动测试远程控制。” Even though I click 'continue session' and again try to scroll down the page, it again shows the same window. 即使我单击“继续会话”并再次尝试向下滚动页面,它也会再次显示相同的窗口。

I am working on MacBook​ Air and using Safari as a browser. 我正在使用MacBook Air并使用Safari作为浏览器。

from selenium import webdriver

import time

browser = webdriver.Safari(executable_path = '/usr/bin/safaridriver')

browser.get('https://www.nytimes.com/search?endDate=20190331&query=cybersecurity&sort=newest&startDate=20180401')

Show_more_xpath = '//*[@id="site-content"]/div/div[2]/div[3]/div/button'

while True:
    try:
        show_more = browser.find_elements_by_xpath('//button[@type="button"], and contains(.,"Show More")')
        time.sleep(2)
        show_more.click()
        time.sleep(5)
    except Exception as e:
        print(e)
        break
print("Complete")
time.sleep(10)

browser.quit()

I want to load "Show More" automatically to get all the entries. 我想自动加载“显示更多”以获取所有条目。 The ultimate goal is to scrape the page using beautifulsoup. 最终目标是使用beautifulsoup刮取页面。

driver.find_element_by_xpath(“(.// * [normalize-space(text())和normalize-space(。)='有搜索反馈吗?让我们知道你的想法。'])[1] / follow :: button [1]“)。单击()

To click on Show More Button do the followings. 要单击“ Show More按钮”,请执行以下操作。

Click on I Accept button first and then use WebdriverWait and element_to_be_clickable to wait for for element to be clickable and then the use action class to click on the specific button.Try to below code hope this will work for you. 首先单击I Accept按钮,然后使用WebdriverWait和element_to_be_clickable等待元素可单击,然后使用操作类单击特定按钮。尝试下面的代码希望这对您有用。

browser.get('https://www.nytimes.com/search?endDate=20190331&query=cybersecurity&sort=newest&startDate=20180401')

browser.find_element_by_xpath("//button[contains(.,'I Accept')]").click()

while True:
    try:
        show_more = WebDriverWait(browser, 5).until(expected_conditions.element_to_be_clickable((By.XPATH, '//button[@type="button"][contains(.,"Show More")]')))
        ActionChains(browser).move_to_element(show_more).click().perform()
    except Exception as e:
        print(e)
        break

print("Complete")
time.sleep(10)

browser.quit()

You need to have following imports to execute above code. 您需要具有以下导入才能执行上述代码。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM