selenium 元素故障

wz3gfoph  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(161)

你好stackoverflow社区,我正面临着一个问题。寻找 selenium 元素。我在python编程和使用谷歌浏览器。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

chrome_driver_path = "C:\Development\chromedriver.exe"
driver = webdriver.Chrome(service=Service(chrome_driver_path))

driver.get("https://tinder.com/")
time.sleep(5)

accept = driver.find_element(By.XPATH, value='//*[@id="c-1351236777"]/div/div[1]/div/div/main/div/div[2]/div/div[3]/div/div/button[2]')

accept.click()
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'value' must be a string
  (Session info: chrome=110.0.5481.104)
Stacktrace:
Backtrace:
    (No symbol) [0x002B6643]
    (No symbol) [0x0024BE21]
    (No symbol) [0x0014DA9D]
    (No symbol) [0x001813F3]
    (No symbol) [0x0018147B]
    (No symbol) [0x001B8DFC]
    (No symbol) [0x0019FDC4]
    (No symbol) [0x001B6B09]
    (No symbol) [0x0019FB76]
    (No symbol) [0x001749C1]
    (No symbol) [0x00175E5D]
    GetHandleVerifier [0x0052A142+2497106]
    GetHandleVerifier [0x005585D3+2686691]
    GetHandleVerifier [0x0055BB9C+2700460]
    GetHandleVerifier [0x00363B10+635936]
    (No symbol) [0x00254A1F]
    (No symbol) [0x0025A418]
    (No symbol) [0x0025A505]
    (No symbol) [0x0026508B]
    BaseThreadInitThunk [0x762600F9+25]
    RtlGetAppContainerNamedObjectPath [0x77167BBE+286]
    RtlGetAppContainerNamedObjectPath [0x77167B8E+238]
    (No symbol) [0x00000000]

这是代码,但它不能运行。我想这是因为html代码的body标记是关闭的,我需要用selenium打开它。实际上,如果我在手动检查代码时右键单击,我可以打开它。然后我单击"expand recursively",代码运行良好,但我不知道如何用python自动打开它。

vuktfyat

vuktfyat1#

I accept元素是一个动态元素,因此要单击***clickable***元素,需要为element_to_be_clickable()引入WebDriverWait,可以使用以下locator strategy

  • 使用 * XPATH *:
driver.get('https://tinder.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button//div[text()='I accept']"))).click()
      • 注意**:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

相关问题