【问题标题】:Using python how do I click a button to scrape the content hidden使用python如何单击按钮来抓取隐藏的内容
【发布时间】:2020-03-31 08:21:39
【问题描述】:

您好,我使用 selenium 编写了一个函数来单击“顾问”按钮,这样我就可以将表格隐藏起来。当我运行它时,我的 chrome 驱动程序成功打开并访问了该页面..但是没有点击该按钮。 希望各位大神帮我解答一下? 注意:我是抓取技术的新手。如果这可以通过bs4 完成,请告诉我。这是一个代码:

def scrapper():
    u = "https://teqatlas.com/products-and-services/0chain"
    browser = webdriver.Chrome(executable_path=binary_path)
    wait = WebDriverWait(browser, 10)
    browser.set_page_load_timeout(10)
    # stop load after a timeout
    try:
        browser.get(u)
    except TimeoutException:
        browser.execute_script("window.stop();")
        
    button = browser.find_element_by_xpath('//button[@class="o5ph61-3 eBqrHG"]')
    if button:
        button.click()

scrapper() 

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:
    from selenium import webdriver
    import pandas as pd
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.add_argument('--headless')
    driver = webdriver.Firefox(options=options)
    
    driver.get("https://teqatlas.com/products-and-services/0chain")
    
    btn = driver.find_element_by_css_selector("button.o5ph61-3.faMQuX").click()
    df = pd.read_html(driver.page_source)[0]
    
    df.to_csv("data.csv", index=False)
    
    driver.quit()
    

    输出:view-online

    【讨论】:

    • 让我安装firefox,对不起
    • @SabbirTalukdar 您可以使用 Chrome、Edge、Firefox 和 Safari。检查docs 以下载您喜欢的驱动程序。
    • WebDriverException: 消息:'geckodriver' 可执行文件需要在 PATH 中。
    • @SabbirTalukdar 您需要将geckodriver 放入您的Python 安装文件夹中。
    【解决方案2】:

    您使用的 xpath 不正确。请选择下面的xpath点击按钮:

    WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Advisories']"))).click()
    

    您需要添加以下导入:

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

    【讨论】:

    • 非常感谢,我会实现代码并让你知道结果
    • @SabbirTalukdar 请选择更新后的答案,我在其中添加了明确的等待。
    【解决方案3】:

    这行得通吗?我也是硒的新手。我在这里将 find_element_by_xpath 更改为 find_element_by_class_name。

    from selenium.webdriver.common.action_chains import ActionChains
    def business_description_scrapper():
        u = "https://teqatlas.com/products-and-services/0chain"
        browser = webdriver.Chrome(executable_path=binary_path)
        wait = WebDriverWait(browser, 10)
        browser.set_page_load_timeout(10)
        # stop load after a timeout
        try:
            browser.get(u)
        except TimeoutException:
            browser.execute_script("window.stop();")
    
        button = browser.find_element_by_class_name("o5ph61-3.eBqrHG")
        if button:
            actions = ActionChains(browser)
            actions.click(button).perform()
    
    business_description_scrapper() 
    

    我测试了一下,按钮被点击了。

    【讨论】:

    • 非常感谢,我会实现代码并让你知道结果
    • 是否找到按钮,无法点击?还是找不到按钮?
    • 一个按钮被点击,但它不是“建议”按钮..
    • @Sabbir 现在怎么样?
    • 这是最麻烦的事情.. 没有错误信息出现
    猜你喜欢
    • 1970-01-01
    • 2020-02-23
    • 2021-12-07
    • 2017-08-28
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2018-03-27
    相关资源
    最近更新 更多