试图废弃网站使用硒,但得到这个错误


type hefrom selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
s=Service('I:chromedriver_win32chromedriver.exe')
path='I:chromedriver_win32chromedriver.exe'
#Website to scrap
website='https://www.adamchoi.co.uk/overs/detailed'

driver=webdriver.Chrome(service=s,options=chrome_options)
driver.get(website)

#Locating and clicking an element
all_matches_button=driver.find_element(by='xpath',value="//label[normalize-space()='All matches']").click()

matches=driver.find_elements(by="xpath",value='tr')
for match in matches:
print(match.text)

错误:"USB: usb_device_handle_win。cc:1045从节点连接中读取描述符失败:连接到系统的设备不正常。(0 x1f)";蓝牙:bluetooth_adapter_winrt。cc:1074获取默认适配器失败。">

解决我的问题

  1. 你怎么得到usb错误和蓝牙?发生什么事了?

  2. type hefrom selenium import webdriver从哪里复制粘贴?

  3. 变量all_matches_button从未在您的代码中使用过,并且当单击元素时不需要保存任何内容到变量

    #Locating and clicking an element
    all_matches_button=driver.find_element(by='xpath',value="//label[normalize-space()='All matches']").click()
    
  4. 这里是开始深入挖掘的工作代码

    from time import sleep
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_experimental_option("detach", True)
    website = 'https://www.adamchoi.co.uk/overs/detailed'
    driver = webdriver.Chrome(options=chrome_options)
    driver.get(website)
    sleep(4)
    tr_elements = driver.find_elements(By.XPATH, "//tr")
    for tr in tr_elements:
    print(tr.tag_name, tr.get_attribute('textContent'))
    

但是,此消息不应阻止您获得抓取结果。尝试在最后一行添加斜杠

来更正xpath。
matches=driver.find_elements(by="xpath",value='//tr')

最新更新