当前位置: 首页 > 知识库问答 >
问题:

Selenium:如何单击“显示”按钮、刮除HREF,然后再次单击“显示”按钮?

颛孙英才
2023-03-14

链接到我正在尝试刮取的页面:

https://www.nytimes.com/reviews/dining

因为这个页面有一个“show more”按钮,所以我需要Selenium自动反复单击“show more”按钮,然后以某种方式使用Beauty soup来获取页面上每个餐厅评论的链接。在下面的照片中,我想获取的链接位于https://...onigiri.html"

迄今为止的代码:

url = "https://www.nytimes.com/reviews/dining"
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
driver.get(url)

for i in range(1):
  button = driver.find_element_by_tag_name("button")
  button.click()

我如何使用WebDriver等待和美丽汤[美丽汤(driver.page_source,'html.parser')]来完成这个任务?

共有2个答案

祁默
2023-03-14

这是一个懒惰加载应用程序。要单击显示更多按钮,您需要使用无限循环和向下滚动页面,然后单击并等待一段时间加载页面,然后存储列表中的值。验证前后列表是否匹配,然后从无限循环中断。

代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time

driver=webdriver.Chrome()
driver.get("https://www.nytimes.com/reviews/dining")
#To accept the coockie click on that
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Accept']"))).click()
listhref=[]

while(True):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"a.css-gg4vpm")))
    lenlistbefore=len(listhref)
    for ele in elements:
        if ele.get_attribute("href") in listhref:
            continue
        else:
            listhref.append(ele.get_attribute("href"))

    lenlistafter = len(listhref)

    if lenlistbefore==lenlistafter:
        break

    button=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//button[text()='Show More']")))
    driver.execute_script("arguments[0].click();", button)
    time.sleep(2)
print(len(listhref))
print(listhref)

注意:-我得到列表计数499

阚砚文
2023-03-14

转到https://www.nytimes.com/reviews/dining按F12,然后按Ctrl Shift C获取元素显示更多,然后如图所示获取元素的xpath:

要查找xpath,请查看:

https://www.techbeamers.com/locate-elements-selenium-python/#locate-xpath中的元素

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def executeTest():
    global driver
    driver.get('https://www.nytimes.com/reviews/dining')
    time.sleep(7)
    element = driver.find_element_by_xpath('Your_Xpath')
    element.click()
    time.sleep(3)

def startWebDriver():
    global driver
    options = Options()
    options.add_argument("--disable-infobars")
    driver = webdriver.Chrome(chrome_options=options)

if __name__ == "__main__":
    startWebDriver()
    executeTest()
    driver.quit()
 类似资料:
  • 按钮点击页面后刷新并重定向到另一个页面。在我的情况下,点击按钮后,我给页面刷新超时,但点击按钮后,它显示超时异常,没有时间等待。 例外: 失败:createEventTest组织。openqa。硒。TimeoutException:等待页面加载时超时。命令持续时间或超时:15.08秒构建信息:版本:“2.53.0”,修订版:“35ae25b1534ae328c771e0856c93e187490c

  • 问题内容: 我正在尝试使用Apple的 SwiftUI 制作一个应用程序,我需要有两个按钮在同一行中显示两个不同的视图。 我使用 Xcode beta 7 和 MacOS Catalina beta 7 。我尝试添加一个表示视图的视图,但是无法单击它,而当我尝试在外部进行简单单击并单击该视图时,该视图没有出现。我也尝试过添加一个内部,但是它也不起作用。单击时也无法添加,视图仍然不会出现 我希望该视

  • 我用的是Android Studio2.2,最小sdk的项目是23。 请帮助我做所需的代码。

  • 在我的程序中,它将单击浏览器中的一个按钮,并且在该页面中,应该会出现另一个按钮。出现该按钮后,我的程序将立即运行下一个操作来单击下一个按钮。我目前收到此错误: ElementNotVisibleException:消息:元素不可见 因此,我假设我正在调用该操作,以便在该按钮出现之前单击下一个按钮。我的问题是,我该怎么做才能让我的程序等到我可以点击按钮,再点击按钮? 这就是我的程序底部的代码的样子。

  • 我有一个按钮(CustomDilaog活动),当点击显示自定义对话框和密码编辑文本,确定按钮和取消按钮时,如果你输入正确的密码,它会打开另一个活动(文本活动),直到现在一切正常, 我有两个部分的问题。 第一部分:当我在(文本活动)并按后退按钮返回(CustomDilaog活动)时,仍然对话框显示在它上面,如何让它关闭 第二部分:对话框启动后,如果我不写密码,只需单击“确定”按钮,edittext为

  • 我的菜单显示向下,但然后水平。 我希望每一个导航项目都显示下来,在彼此的上面。 现场测试站点URL:ministerios-elim.herokuapp.com CodePen:(无法使菜单在单击CodePen时出现)。 https://codepen.io/ogonzales/pen/abnqxlg 更新2: 更新3: