关于find_element 在edge浏览器下的问题,如何解决?


import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
import csv
from selenium.webdriver.common import by as By
driver=webdriver.Edge()   #驱动浏览器
for year in range(1960,2022):  #爬多个网页每一年的,找网址特点
    url="https://www.kuaiyilicai.com/stats/global/yearly/g_gdp/%d.html"%year
    xpath="/html/body/div[2]/div[1]/div[5]/div[1]/div/div/div/table"       #复制找到表格的路径
    driver=driver.get(url)
    table=driver.find_element(By.XPATH,xpath).get_attribute("innerHTML")  #获取表格代码
     
    with open("d:/gdp.csv","a",newline="") as o:    #写文件,把w换成a,避免之前写的文件被覆盖
        csv_writer=csv.writer(o,dialect="excel")
        soup=BeautifulSoup(table,"html,parser")
        table=soup.find_all("tr")   #找行
        for row in table:
            cols=[col.text for col in rows.find_all("td")]   #找列
            if len(cols)==0 or not cols[0].isdigit():       #根据列表索引,删除没有参与排名的国家
                continue
            cols.append(year)               #追加年份标注
            csv_weiter.writerow(cols)      #一行一行的写
driver.close()

img

好问题!!抱歉我也不太懂,你问问chatGPT吧:https://new.quke123.com/
或者问下其他Python群友:https://app.yinxiang.com/fx/13ce6bbd-f36f-4e92-be53-92dd381ed729

问题就是浏览器驱动没启动成功。第7行代码没有通过executable_path参数指定浏览器的驱动位置,你已经将浏览器驱动放到系统环境变量path中或者放到python安装目录下的Scripts文件中了吗?没有的话指定下,比如像这样:
webdriver.Edge(r"E:\test\browsers\MicrosoftWebDriver.exe")

有帮助的话,请点采纳该答案~

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^