pycharm 使用Selenium和Python的NoSuchElementException

rdlzhqv9  于 2023-03-18  发布在  PyCharm
关注(0)|答案(3)|浏览(185)

我正在尝试使用selenium和python登录网站。我在IDE中使用PyCharm。我找到了登录按钮的类名,但当我使用以下命令搜索并尝试单击它时:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

notsure=Service(r"C:\Users\kumpd\OneDrive\Desktop\Project\chromedriver_win32\chromedriver.exe")
website = "https://buff.market/market/goods/1?game=csgo"

path = r"C:\Users\kumpd\OneDrive\Desktop\Project\chromedriver_win32\chromedriver.exe"

driver = webdriver.Chrome(service=notsure)

driver.get(website)

time.sleep(10)

login = driver.find_element(By.CLASS_NAME, "w-button c-nav-sign w-button--primary w-button--l")
login.click()

driver.quit()

以下是网站的HTML:enter image description here
出现以下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".w-button c-nav-sign w-button--primary w-button--l"}
Backtrace:
    (No symbol) [0x0026DCE3]
    (No symbol) [0x002039D1]
    (No symbol) [0x00114DA8]
    (No symbol) [0x0014019F]
    (No symbol) [0x001403AB]
    (No symbol) [0x0016EE62]
    (No symbol) [0x0015AF14]
    (No symbol) [0x0016D57C]
    (No symbol) [0x0015ACC6]
    (No symbol) [0x00136F68]
    (No symbol) [0x001380CD]
    GetHandleVerifier [0x004E3832+2506274]
    GetHandleVerifier [0x00519794+2727300]
    GetHandleVerifier [0x0051E36C+2746716]
    GetHandleVerifier [0x00316690+617600]
    (No symbol) [0x0020C712]
    (No symbol) [0x00211FF8]
    (No symbol) [0x002120DB]
    (No symbol) [0x0021C63B]
    BaseThreadInitThunk [0x76AE7D69+25]
    RtlInitializeExceptionChain [0x77B0BB9B+107]
    RtlClearBits [0x77B0BB1F+191]
    (No symbol) [0x00000000]

你知道为什么 selenium 元素找不到按钮吗?谢谢你的帮助!
正如你所看到的,我已经尝试使用时间睡眠来迫使网页等待。

j5fpnvbx

j5fpnvbx1#

试试这个代码

driver.find_element_by_class_name("w-button.c-nav-sign.w-button--primary.w-button--l")

如果一直出现该错误,请尝试此代码

driver.find_element_by_xpath("//*[text()='write text on button']").click()
kdfy810k

kdfy810k2#

要单击以<button>标记为目标的元素Sign in insteadog,您可以更深入一步,以<span>标记为目标,并为element_to_be_clickable()引入WebDriverWait,您可以使用以下locator strategies之一:

  • 使用 *CSS选择器 *:
driver.get("https://buff.market/market/goods/1?game=csgo")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.w-button.c-nav-sign.w-button--primary.w-button--l > span"))).click()
  • 使用 XPATH
driver.get("https://buff.market/market/goods/1?game=csgo")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='w-button c-nav-sign w-button--primary w-button--l']/span"))).click()

*注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • 浏览器快照:

ippsafx7

ippsafx73#

我过去遇到过类似的错误。大多数情况下,它与Chrome驱动程序版本有关。请确保您安装的Chrome驱动程序版本与您的Chrome浏览器版本相匹配。

我的chrome浏览器喜欢自己更新,然后我必须再次更新Chrome驱动程序,通常当我得到一个错误,看起来就像这样。可能无法修复问题,但可能会有所帮助

相关问题