爱新觉罗·卜 2020-11-05 22:32 采纳率: 0%
浏览 38

关于python的selenium运用,实在是没辙了,跪求大佬帮看看什么原因,

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

# 选择登录账户
user = "川建"
# 待统计任务对应分享链接
share_link = "https://wss.pet/s/3wlx4vq6208"

# 各用户登录信息(账号密码)
user_info = {
                "川建":['17724607375', 'panqi559']
            }
login_number = user_info[user][0]
login_password = user_info[user][1]

#不嫌麻烦可以像这样手动添加成员(一劳永逸)
name_list = {
           '杨宇豪':0, '代溟伟':0, '肖卓琦':0, '胡鹏':0, '熊梓旭':0,
           '李洪波':0, '洛桑次仁':0, '陈戌':0, '薛宸熙':0, '刘金炜':0 }

# import pandas as pd
# import numpy as np
# name_list = {}
# data = pd.read_excel("class_data.xlsx")
# data = data.iloc[2:,0]
# data = data.to_numpy()
# for i in range(len(data)):
#     name_list[str(data[i])] = 0;



# import urllib
# def asKURL(url):
#     head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"}
#
#     request = urllib.request.Request(url,headers=head)
#     html = ""
#     try:
#          response = urllib.request.urlopen(request)
#          html = response.read().decode("utf-8")
#          print(html)
#     except urllib.error.URLError as e:
#         if hasattr(e,"code"):
#             print(e.code)
#         if hasattr(e.reason):
#             print(e.reason)
#     return html




chrome_options = Options()
chrome_options.add_argument("--headless")

driver = webdriver.Chrome(options=chrome_options)
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(5)

print("Entering the website...")
url = "https://www.wenshushu.cn/signin"
driver.get(url)
time.sleep(2)

print("Inputing login info...")
driver.find_element_by_xpath("//div[@id='page_content']/div/div/div/div/ul/li[2]").click()
driver.find_element_by_xpath("//input[@type='text']").click()
driver.find_element_by_xpath("//input[@type='text']").clear()
driver.find_element_by_xpath("//input[@type='text']").send_keys(login_number)
driver.find_element_by_xpath("//input[@type='password']").click()
driver.find_element_by_xpath("//input[@type='password']").clear()
driver.find_element_by_xpath("//input[@type='password']").send_keys(login_password)
driver.find_element_by_xpath("//div[@id='page_content']/div/div/div/div[2]/div/div/button/span").click()
time.sleep(2)

print("Locating to the task...")
url = "https://www.wenshushu.cn/f/" + str(share_link[18:])
driver.get(url)

print("Checking submit info...")
time.sleep(3)
page_info = driver.find_elements_by_css_selector("[class='g-upload_user']")
for i in range(len(page_info)):
    name_list[page_info[i].get_attribute('innerText')] = 1
    print(page_info[i].get_attribute('innerText'))

print("Writing detailed list...")
print("unsubmit list:\n")
for name in name_list.keys():
    if(name_list[name] == 0):
        print(name)
print("\nsubmit list:\n")
for name in name_list.keys():
    if(name_list[name] == 1):
        print(name)
driver.close()

print("Check Finished!")

报错如下:

Inputing login info...
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/checkkusi/shouji0000.py", line 68, in <module>
    driver.find_element_by_xpath("//div[@id='page_content']/div/div/div/div/ul/li[2]").click()
  File "D:\pythohuanjingzujian\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "D:\pythohuanjingzujian\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "D:\pythohuanjingzujian\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\pythohuanjingzujian\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id='page_content']/div/div/div/div/ul/li[2]"}
  (Session info: chrome=83.0.4103.14)

网上查了两天都说chrome的版本和Chromedriver的版本不匹。但是反反复复去尝试了,我浏览器也是83.0.4103.14

  • 写回答

2条回答 默认 最新

  • 梦想橡皮擦 Python领域优质创作者 2020-12-10 14:34
    关注

    你这个问题不是 chromedriver 版本问题,是定位不到元素。由于Xpath层级选择的问题需要

    Unable to locate element: {"method":"xpath","selector":"//div[@id='page_content']/div/div/div/div/ul/li[2]"}       
      (Session info: chrome=87.0.4280.66)

    这里的xpath写的不对。我只修改了一处 Xpath,其它的交给你自己啦。

    print("Inputing login info...")
    driver.find_element_by_xpath(
        "//*[@id='page_content']/div[1]/div[1]/ul/li[2]").click()

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试