尝试使用 Selenium WebDriver 查找 Web 元素时出错

Error while trying to locate Web Elements with Selenium WebDriver

提问人:Vasco Marques 提问时间:11/5/2023 最后编辑:Vasco Marques 更新时间:11/8/2023 访问量:51

问:

我正在尝试使用 Selenium Webdriver,但我的代码遇到了一些问题。

我给了页面加载时间,但我似乎仍然找不到元素。 我以为该页面可能会阻止它,但我也尝试了 twitter,但它不起作用(我的一个朋友说他用 twitter 做了一些事情并且它起作用了),所以我不知道。

这是我第一次使用 Selenium,我以为它会更容易,但没有。我还改变了硒获取网络元素的方式,但没有一个修复它!

Chrome 版本:119.0.6045.124 硒 : 4.10.0

我有这个进口(与硒有关):

from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

以下是驱动程序配置:

options = Options()
        options.add_experimental_option("excludeSwitches", ["enable-automation"])
        options.add_experimental_option('useAutomationExtension', False)
        options.add_experimental_option("excludeSwitches", ["enable-logging"])
        options.add_argument('--disable-web-security')
        options.add_argument('--disable-site-isolation-trials')
        options.add_argument('--headless')
        driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options= options)
        driver.get(pageurl)

我有这个代码块:

anchor_url = "https://www.recaptcha.net/recaptcha/api2/anchor?ar=1&k=6LeMGXkUAAAAAOlMpEUm2UOldiq38QgBPJz5-Q-7&co=aHR0cHM6Ly9pbnRlcm5hbC1hcGkucHJvbGlmaWMuY286NDQz&hl=fr&v=gWN_U6xTIPevg0vuq7g1hct0&size=invisible&cb=igv4yino6y0f"
            reCaptcha_response = reCaptchaV3(anchor_url)
            end = time()
            print(f"Captcha solved in {end-start}s")
            wait = WebDriverWait(driver,20)
            try:
                print("trying go find username")
                username = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[2]/div/div/section/div[1]/div/form/div[1]/div/div/input"))) 
                print("found it")
                username.send_keys({config["mail"]})
            except:
                driver.quit()
            password = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/div/section/div[1]/div/form/div[3]/div/div/input")
            captcha = driver.find_element(By.ID, "g-recaptcha-response-100000")
            password.send_keys({config["password"]})
            captcha.send_keys(reCaptcha_response)
            print("\n Done email, password and captcha")
            driver.execute_script(f'document.getElementById("g-recaptcha-response-100000").innerHTML="{reCaptcha_response}";')
            driver.find_element(By.ID, "login").submit()
            print(sleep)
            sleep(3)

我收到的错误是这样的:

File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\util\connection.py", line 95, in create_connection
    raise err
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\util\connection.py", line 85, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connectionpool.py", line 715, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connectionpool.py", line 416, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connection.py", line 244, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 1286, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 1332, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 1281, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 1041, in _send_output
    self.send(msg)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 979, in send
    self.connect()
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connection.py", line 205, in connect
    conn = self._new_conn()
           ^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000224A968F150>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Prolific-Joiner-main\Prolific-Joiner-main\my.py", line 189, in <module>
    p_updater = ProlificUpdater()
                ^^^^^^^^^^^^^^^^^
  File "D:\Prolific-Joiner-main\Prolific-Joiner-main\my.py", line 30, in __init__
    else: self.bearer = "Bearer " + self.get_bearer_token()
                                    ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Prolific-Joiner-main\Prolific-Joiner-main\my.py", line 95, in get_bearer_token
    password = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/div/section/div[1]/div/form/div[3]/div/div/input")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 738, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 342, in execute
    response = self.command_executor.execute(driver_command, params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\remote_connection.py", line 297, in execute
    return self._request(command_info[0], url, body=data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\remote_connection.py", line 318, in _request
    response = self._conn.request(method, url, body=body, headers=headers)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\request.py", line 81, in request
    return self.request_encode_body(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\request.py", line 173, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\poolmanager.py", line 376, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connectionpool.py", line 827, in urlopen
    return self.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connectionpool.py", line 827, in urlopen
    return self.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connectionpool.py", line 827, in urlopen
    return self.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\connectionpool.py", line 799, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\vasco\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\urllib3\util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=52215): Max retries exceeded with url: /session/c36d583758b90867677dc53fef46febf/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000224A968F150>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

这是我第一次使用 Selenium,我已经处理了在页面中找不到元素的错误,我知道我需要让页面加载到驱动程序中。我还改变了硒的方式并获取网络元素,但没有一个工作!

python-3.x 硒网络 selenium-web驱动程序-python

评论

0赞 moken 11/5/2023
一个最小的可重现示例应该尝试重现你得到的错误。还要注意你的缩进,“代码块”的第一行之后的每一行都无缘无故地缩进了两次,大概是这样。如果还将它们格式化为代码,则回溯通常更易于阅读。
0赞 Vasco Marques 11/5/2023
在代码中,缩进很好,小缩进偏移量和 vscode 组成了一个大的红色标记。当你说如果我将它们格式化为代码,tracebakcs 更容易阅读时,你的意思是在这里插入它也有一个代码块吗?
0赞 moken 11/5/2023
是的,代码块格式有助于提高错误文本的可读性。
0赞 pcalkins 11/7/2023
包括使用的 Selenium、Chromedriver 和 Chrome 版本。您可能需要 options.add_argument(“--remote-allow-origins=*”);
0赞 Vasco Marques 11/8/2023
已经将 Tracebacks 制作为代码块。关于版本,chrome 和 selenium 已经在那里了,但 chrome webdriver 我无法分辨。我已经下载了它,并且我把它放在同一个文件夹中,但它从未要求我提供路径或类似的东西!由于我在网络驱动程序“ChromeService(ChromeDriverManager().install()”中安装了这个,因此我认为它正在安装或使用网络驱动程序。

答: 暂无答案