簡體   English   中英

CssSelector 是我可以用於 shadowroot 的唯一方法嗎? selenium.common.exceptions.InvalidArgumentException:消息:無效參數:無效定位器

[英]Is CssSelector the only way I can use for shadowroot? selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

CSS_SELECTOR 是我可以用來在 shadowroot 元素中查找元素的唯一方法嗎? 我正在使用 Python 3.10.1、Selenium (4.1.0) 鉻 (96.0.4664.110)。

下面是我的代碼

host = browser.find_element(By.ID, 'someid') 
shadow_root = browser.execute_script('return arguments[0].shadowRoot', host)
goal = shadow_root.find_element(By.XPATH, '//*/button[@aria-label="Required"]')

如果我不使用By.CSS_SELECTOR ,我得到的錯誤如下。

Traceback (most recent call last):
  File "C:\Files\Workspace\PostInstallVerify.py", line 787, in <module>
    maxlenInput = getShadowElement(maxlenid, ".rwa-input")
  File "C:\Files\Workspace\PostInstallVerify.py", line 272, in getShadowElement
    goal = shadow_root.find_element(By.XPATH, '//*/button[@aria-label="Required"]')
  File "C:\Users\Tomas.Tang\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\shadowroot.py", line 45, in find_element
    return self._execute(Command.FIND_ELEMENT_FROM_SHADOW_ROOT, {"using": using, "value": value})['value']
  File "C:\Users\Tomas.Tang\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\shadowroot.py", line 64, in _execute
    return self.session.execute(command, params)
  File "C:\Users\Tomas.Tang\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Tomas.Tang\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
  (Session info: chrome=96.0.4664.110)
Stacktrace:
Backtrace:
    Ordinal0 [0x00326903+2517251]
    Ordinal0 [0x002BF8E1+2095329]
    Ordinal0 [0x001C2848+1058888]
    Ordinal0 [0x001ED5C4+1234372]
    Ordinal0 [0x001ED68B+1234571]
    Ordinal0 [0x001E3AE1+1194721]
    Ordinal0 [0x0020650A+1336586]
    Ordinal0 [0x001E3A36+1194550]
    Ordinal0 [0x002065BA+1336762]
    Ordinal0 [0x00215BBF+1399743]
    Ordinal0 [0x0020639B+1336219]
    Ordinal0 [0x001E27A7+1189799]
    Ordinal0 [0x001E3609+1193481]
    GetHandleVerifier [0x004B5904+1577972]
    GetHandleVerifier [0x00560B97+2279047]
    GetHandleVerifier [0x003B6D09+534521]
    GetHandleVerifier [0x003B5DB9+530601]
    Ordinal0 [0x002C4FF9+2117625]
    Ordinal0 [0x002C98A8+2136232]
    Ordinal0 [0x002C99E2+2136546]
    Ordinal0 [0x002D3541+2176321]
    BaseThreadInitThunk [0x766CFA29+25]
    RtlGetAppContainerNamedObjectPath [0x773A7A9E+286]
    RtlGetAppContainerNamedObjectPath [0x773A7A6E+238]
    (No symbol) [0x00000000]

只需使用

 .//button[@aria-label='Required']

Xpath 從一個元素需要一個。 不像其他人。

您可以通過任何常規定位器在 shadow DOM 中定位元素:CSS_SELECTOR、XPATH、ID、CLASS_NAME 等。
在這里,您嘗試使用無效的 XPath 表達式。

'//*/button[@aria-label="Required"]'

不是有效的 XPath 表達式。
我不知道您正在處理哪個頁面以及該元素的正確定位器是什么,但這至少是一個正確的 XPath 表達式,可能對您來說是正確的:

'//button[@aria-label="Required"]'

所以而不是

goal = shadow_root.find_element(By.XPATH, '//*/button[@aria-label="Required"]')

嘗試這個:

goal = shadow_root.find_element(By.XPATH, '//button[@aria-label="Required"]')

或者可能

goal = shadow_root.find_element(By.XPATH, './/button[@aria-label="Required"]')

不是與shadowroot交互的唯一方式。

您的代碼試驗接近完美,但XPATH表達式不是有效的 xpath 表達式:

goal = shadow_root.find_element(By.XPATH, '//*/button[@aria-label="Required"]')

/*不需要,因為//button本身會在整個DOM Tree中查找按鈕標記。 實際上,您的代碼行將是:

goal = shadow_root.find_element(By.XPATH, '//button[@aria-label="Required"]')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Python selenium 用於撰寫評論谷歌地圖(selenium.common.exceptions.InvalidArgumentException:消息:無效參數:無效定位器) selenium.common.exceptions.InvalidArgumentException:消息:無效參數:使用 selenium webdriver 添加 cookie 時缺少“cookie” selenium.common.exceptions.InvalidArgumentException:消息:無效參數:在 Selenium 中切換幀時缺少“元素”錯誤 URL 必須是字符串 selenium - selenium.common.exceptions.InvalidArgumentException:消息:無效參數:&#39;url&#39; 必須是字符串 selenium.common.exceptions.InvalidArgumentException:消息:使用 Selenium Webdriver 通過 Python 調用 get() 時參數無效錯誤 selenium.common.exceptions.InvalidArgumentException:消息:無效參數:用戶數據目錄已在使用真正的 Chrome 瀏覽器時出錯 selenium.common.exceptions.InvalidArgumentException:消息:無效參數:“使用”必須是使用等待和預期條件的字符串 selenium.common.exceptions.InvalidArgumentException:消息:無效參數錯誤調用 get() 與使用 Selenium Python 從文本文件讀取的 url selenium.common.exceptions.InvalidArgumentException:消息:無效參數:使用 Selenium 調用 send_keys() 時未找到文件錯誤 selenium.common.exceptions.InvalidArgumentException:消息:無效參數:使用 Selenium 上傳文件時找不到文件錯誤 Python
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM