selenium.common.exceptions.NoSuchElementException: 消息: 没有这样的元素: 无法定位元素: {"method":"css selector","selector":".ui flu~"}

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ui flu~"}

这是我使用的代码:

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 中通过其 class 名称找到一个按钮,以下载最新版本的 zip 文件。

截图:

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

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

而不是

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

因为您复制的 class 名称是该对象的 class 名称的汇编。每个 class 名称由 space 分隔。因此,使用最独特的标识符 'primary' 我们能够将元素缩小到 3 个,并看到您想要的元素是最后一个。

然后

b.click()

单击该元素。

尽管您可能想要等待该元素加载到页面上。

根据 HTML:

将带有文本的可点击元素识别为创建下载包 您可以使用以下任一项 :

  • 使用 xpath:

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

最好点击你需要诱导的元素WebDriverWait for the and you can use either of the following :

  • 使用 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