繁体   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