python selenium.common.exceptions.NoSuchElementException:Message:no such element:无法定位元素:{“method”:“css selector”,“selector”:“.ui flu~"}

hmmo2u0o  于 2023-05-16  发布在  Python
关注(0)|答案(3)|浏览(379)

这是我使用的代码:

import requests as r, sys as sus, bs4 as bs, webbrowser as wb
from selenium import webdriver as wd

dr = wd.Chrome()

b = r.get("https://uupdump.net/fetchupd.php?arch=amd64&ring=wif&build=latest").text
s = bs.BeautifulSoup(b, features="html.parser")

if "/selectlang.php?id=" in b:
    l = b.split("/selectlang.php?id=")[1].split('"')[0]
    u = f"https://uupdump.net/download.php?id={l}&pack=es-es&edition=professional"
    print(u)
    b = r.get(u).text
    s = bs.BeautifulSoup(b, features="html.parser")
    print(s)
    dr.get(u)
    b = dr.find_element_by_class_name('ui fluid right labeled icon primary button')

这就是错误:

uupdump.py:17: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  b = dr.find_element_by_class_name('ui fluid right labeled icon primary button')
Traceback (most recent call last):
  File "C:\Users\Aritz\Downloads\thign\uupdump.py", line 17, in <module>
    b = dr.find_element_by_class_name('ui fluid right labeled icon primary button')
  File "C:\Users\Aritz\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 760, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Users\Aritz\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Aritz\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Aritz\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ui fluid right labeled icon primary button"}

我想使用Selenium从uupdump.net中通过其类名找到一个按钮,以下载最新版本的zip文件。
截图:

piok6c0g

piok6c0g1#

根据HTML:

要将带有文本的***可点击***元素标识为***创建下载包***,可以使用以下Locator Strategies之一:

  • 使用 xpath
b = dr.find_element(By.XPATH, "//button[@class='ui fluid right labeled icon primary button' and contains(., 'Create download package')]")

理想情况下,要单击需要为element_to_be_clickable()诱导WebDriverWait的元素,可以使用以下Locator Strategies之一:

  • 使用 XPATH
b = WebDriverWait(dr, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui fluid right labeled icon primary button' and contains(., 'Create download package')]")))

*注意:需要添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
uz75evzq

uz75evzq2#

要找到您想要的特定按钮元素,您可以执行以下操作:

b = dr.find_elements_by_class_name('primary')[-1]

而不是

b = dr.find_element_by_class_name('ui fluid right labeled icon primary button')

因为您复制的类名是该对象的类名的汇编。每个类名之间用空格分隔。因此,使用最唯一的标识符'primary',我们能够将元素缩小到3个,并看到您想要的元素是最后一个。
然后做

b.click()

点击这个元素。
不过您可能希望等待该元素加载到页面上。

k4emjkb1

k4emjkb13#

在所有这些情况下,您需要包括/增加等待时间语句,以便网页在浏览器中完全加载。
对于我的场景,time.sleep()不存在,我添加了它,然后逐渐将时间增加到60秒,错误得到解决。下面是我得到的错误副本

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="performance"]/div[1]/div[1]/div[1]/a/div[2]"}
  (Session info: headless chrome=112.0.5615.138)

相关问题