【问题标题】:Find broken links in python with Selenium. No supplied schema error使用 Selenium 在 python 中查找损坏的链接。未提供架构错误
【发布时间】:2019-01-24 20:03:47
【问题描述】:

我正在尝试在页面中查找损坏的链接。我正在使用这段代码: (我没有提供原始 URL 和元素 ID 数据,因为它是机密信息,并且我使用显式等待,因为它需要在访问页面之前登录)

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

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, 
executable_path='C:\\Chromedriver\\chromedriver.exe')
driver.get('https://pagename.com')
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, 
'elementID')))
links = driver.find_elements_by_css_selector("a")
for link in links:
    r = requests.head(link.get_attribute('href'))
    print(link.get_attribute('href'), r.status_code)

问题是这段代码适用于大多数页面。但是我正在使用的页面在 href 中没有完整的 URL,它只有“/扩展”。所以我得到 requests.exceptions.MissingSchema:无效的 URL “无”:未提供架构。也许你的意思是http://None?错误,我在使用 href 加入 URL 时遇到了麻烦。如何在循环中加入带有 href 的 URL?

【问题讨论】:

    标签: python selenium testing automation webdriver


    【解决方案1】:

    试试

    r = requests.head("https://pagename.com/"+link.get_attribute('href'));
    print(("https://pagename.com/"+link.get_attribute('href')), r.status_code)
    

    【讨论】:

      【解决方案2】:

      “+”连接运算符可以工作。

      baseURL = "https://domainName.com/resource/" 
      for link in links:
      r = requests.head(link.get_attribute('href'))
      actualURL=baseURL + link.get_attribute('href')
      

      【讨论】:

        猜你喜欢
        • 2018-11-04
        • 2016-05-02
        • 1970-01-01
        • 2019-06-16
        • 2015-07-21
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        相关资源
        最近更新 更多