【问题标题】:Not Clicking and Copying element in Selenium python 3在 Selenium python 3 中不单击和复制元素
【发布时间】:2021-10-08 03:32:44
【问题描述】:

大家好,我想从这个网站上抓取姓名和电话号码,但它没有点击和复制查看电话号码所需的“点击显示”元素。同样在此之后,我如何在循环中添加多个(100+)url,我可以用 bs4 实现相同的效果,因为它会更快。

from selenium import webdriver
chrome_path = r"C:\Users\lenovo\Downloads\chromedriver_win32 (5)\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.autotrader.ca/a/ram/1500/hamilton/ontario/19_12052335_/?showcpo=ShowCpo&ncse=no&ursrc=pl&urp=2&urm=8&sprx=-2")

driver.find_element_by_xpath('//p[@class="hero-title"]').text
'2011 Ram 1500 Crew Cab Sport'
driver.find_element_by_xpath('//a[@class="link ng-star-inserted"]').click()

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a _ngcontent-wfx-c190="" href="javascript:void(0)" class="link ng-star-inserted">...</a> is not clickable at point (1079, 593). Other element would receive the click: <div id="cookie-banner" class="container-fluid cookie-banner" style="display: block;">...</div>

【问题讨论】:

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


    【解决方案1】:

    关于Click to Show

    你需要关闭Cookie setting弹窗,然后执行scrollIntoView点击元素。

    能够使用以下代码点击Click to Show

    from selenium import webdriver
    import time
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
    driver.maximize_window()
    driver.implicitly_wait(10)
    
    driver.get("https://www.autotrader.ca/a/ram/1500/hamilton/ontario/19_12052335_/?showcpo=ShowCpo&ncse=no&ursrc=pl&urp=2&urm=8&sprx=-2")
    
    wait =WebDriverWait(driver,30)
    
    wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@class='close-button']"))).click()
    option = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[text()= 'Click to show']")))
    driver.execute_script("arguments[0].scrollIntoView(true);",option)
    option.click()
    time.sleep(10)
    

    关于对多个 URL 做同样的事情:

    你可以尝试如下:

    urls = ['url1','url2']
    for url in urls:
        driver.get(url)
        ...
    

    如果你想在Beautifulsoup 中提出这个问题,你需要在Beautifulsoup 标签下提出这个问题。现在你已经标记了pythonseleniumweb-scraping

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 2020-07-09
      • 2019-03-19
      • 2020-05-24
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多