【问题标题】:Python : Unable to locate element while scraping in seleniumPython:在硒中刮擦时无法定位元素
【发布时间】:2019-12-01 12:09:30
【问题描述】:

我正在尝试从页面中抓取电话号码。一个这样的页面是this。所有页面都包含一个带有文本SEE PHONE NUMBER 的链接按钮,点击它会显示电话号码。我正在尝试抓取那个特定的电话号码。这是我迄今为止尝试过的:

company_url = 'https://www.europages.co.uk/PORT-INTERNATIONAL-GMBH/00000004710372-508993001.html'
d = {}
try :
    options = webdriver.FirefoxOptions()
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--incognito')
    options.add_argument('--headless')
    driver = webdriver.Firefox(options=options)
    driver.get(company_url)
    link = driver.find_element_by_link_text('See phone number')
    link.click()
    driver.close()
    page = driver.page_source
    soup = bs(page, 'html.parser')
    tel_no = soup.find('div', {'class' : 'info-tel-num'})
    tel_no = tel_no.text
    d['telephone'] = tel_no
except Exception as e:
    print(f'Error encountered : {e}')

但每次,它都会在异常块中打印此错误:

遇到错误:消息:无法定位元素:查看电话号码

这个链接按钮没有任何特定的 id 或类,所以我不能使用find_element_by_idfind_element_by_class。这是我通过该按钮上的检查元素发现的(在单击之前):

这是单击按钮后检查元素的结果:

如何刮取这个电话号码?我做错了什么?

【问题讨论】:

  • 您尝试添加一些等待吗?
  • 如果之前出现故障,在link.click() 之后会有什么帮助?这可能是页面加载时间问题,您需要等待才能尝试定位元素。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

所需元素是启用了JavaScript 的元素,因此要在元素上定位和click(),您必须为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='EpGetInfoTel']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'EpGetInfoTel') and text()='See phone number']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 要抓取电话号码,您可以使用以下代码行:

    print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//a[starts-with(@onclick, 'EpGetInfoTel') and text()='See phone number']//following::div[1]"))).get_attribute("innerHTML"))
    
  • 控制台输出:

    +49 04 03 01 00 00
    
  • 浏览器快照:

【讨论】:

  • 第一个也是最准确的答案!这个应该被接受。
【解决方案2】:

用这个点击查看电话号码

$("[itemprop='telephone'] a")[0].click();

要获取电话号码值,请使用:

$("[itemprop='telephone'] [style='display: block;']")[0].innerText

【讨论】:

    【解决方案3】:

    使用WebDriverWait 并单击具有以下xpath 的元素。然后如果您想使用BeautifulSoup,请获取page_source

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from bs4 import BeautifulSoup as bs
    company_url = 'https://www.europages.co.uk/PORT-INTERNATIONAL-GMBH/00000004710372-508993001.html'
    d = {}
    try :
        options = webdriver.FirefoxOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--incognito')
        options.add_argument('--headless')
        driver = webdriver.Firefox(options=options)
        driver.get(company_url)
        link =WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//a[contains(.,"See phone number")]')))
        link.click()
        time.sleep(2)
        page = driver.page_source
        driver.close()
        soup = bs(page, 'html.parser')
        tel_no = soup.find('div', {'class' : 'info-tel-num'})
        tel_no = tel_no.text
        d['telephone'] = tel_no
    except Exception as e:
       print(f'Error encountered : {e}')
    
    
    print(d)
    

    控制台输出:

    {'telephone': '+49 04 03 01 00 00'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多