excel 如何在shadow-root(已关闭)主机中定位元素

6vl6ewon  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(191)

我的代码。我有一个Excel数据,并希望在该URL上搜索这些,但VSCode无法找到元素。为什么它找不到那个元素?我尝试了CSS选择器,ID,XPath,但没有工作。

import openpyxl
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

path = "dvt.xlsx"

driver = webdriver.Chrome()
url = "https://www.boschaftermarket.com/xc/en/keysecure.html"
driver.implicitly_wait(5)
driver.get(url)

wb = openpyxl.load_workbook(path)
sheet = wb.active

sayac = 1

while sayac <= 14109:
    str_getVal = "A" + str(sayac)
    # Bir sayaç başlat
    str_setval = "B" + str(sayac)

    input = driver.find_element(By.ID, "search_input")
    input.send_keys(sheet[str_getVal].value)

    button = driver.find_element(By.ID, "search_button")
    button.click()
    result = driver.find_element(
        By.XPATH, '//*[@id="rbgfCheckResponse"]/div/div/text()'
    ).text

    sheet[str_setval] = result

    sayac += 1

wb.save(path)

driver.quit()

我的代码给出了这些错误。我搜索了更多,但我不能正确地做到这一点。可以用vscode吗?我不使用ide btw。我不知道。谢谢

DevTools listening on ws://127.0.0.1:61704/devtools/browser/fc1c3637-7cf4-4506-ae63-e562ce805db3       
[18836:23004:1006/092059.818:ERROR:device_event_log_impl.cc(225)] [09:20:59.820] USB: usb_service_win.cc:415 Could not read device interface GUIDs: Sistem belirtilen dosyay² bulam²yor. (0x2)
Traceback (most recent call last):
  File "c:\Users\isra0\OneDrive\Belgeler\VS Codes Projects\PythonProject\main.py", line 28, in <module>
    input = driver.find_element(By.ID, "search_input")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\isra0\AppData\Local\Programs\Python\Python312\Lib\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\isra0\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in execute
    self.error_handler.check_response(response)
  File "C:\Users\isra0\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="search_input"]"}
  (Session info: chrome=117.0.5938.134); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
        GetHandleVerifier [0x00007FF7B81B7D12+55474]
        (No symbol) [0x00007FF7B81277C2]
        (No symbol) [0x00007FF7B7FDE0EB]
        (No symbol) [0x00007FF7B801EBAC]
        (No symbol) [0x00007FF7B801ED2C]
        (No symbol) [0x00007FF7B8059F77]
        (No symbol) [0x00007FF7B803F19F]
        (No symbol) [0x00007FF7B8057EF2]
        (No symbol) [0x00007FF7B803EF33]
        (No symbol) [0x00007FF7B8013D41]
        (No symbol) [0x00007FF7B8014F84]
        GetHandleVerifier [0x00007FF7B851B762+3609346]
        GetHandleVerifier [0x00007FF7B8571A80+3962400]
        GetHandleVerifier [0x00007FF7B8569F0F+3930799]
        GetHandleVerifier [0x00007FF7B8253CA6+694342]
        (No symbol) [0x00007FF7B8132218]
        (No symbol) [0x00007FF7B812E484]
        (No symbol) [0x00007FF7B812E5B2]
        (No symbol) [0x00007FF7B811EE13]
        BaseThreadInitThunk [0x00007FFA5E7A257D+29]
        RtlUserThreadStart [0x00007FFA5FB4AA78+40]
qnakjoqk

qnakjoqk1#

元素被放置在shadow-root中,状态为closed
当影子根的模式为“closed”时,影子根的实现内部是不可访问和不可更改的,因此您无法访问shadowRoot属性。
由于Selenium实现了CDP协议API,因此在页面加载时,可以使用open状态覆盖shadow-rootclosed状态。
您只需要覆盖Element类原型方法attachShadow,并在页面加载之前在新文档上评估脚本。
How to override Element properties
How to executed script on load via CDP Protocol

代码示例:

url = "https://www.boschaftermarket.com/xc/en/keysecure.html"
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': """
Element.prototype._attachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function () {
    return this._attachShadow( { mode: "open" } );
};
"""})
driver.get(url)
closed_shadow_host = driver.find_element(By.CSS_SELECTOR, "aa-dxt-qr-content")
shadow_root = driver.execute_script('return arguments[0].shadowRoot', closed_shadow_host)
input = shadow_root.find_element(By.ID, "search_input")

另一种选择(您的具体情况),您可以使用JS执行器访问包含shadow-root结构的root属性。
在这种情况下,来自封闭shadow-root的元素是不可交互的,因此要与它们交互,您应该使用ActionChains
About shadow-root
About closed state

from selenium.webdriver import ActionChains

# previous code

action = ActionChains(driver)

closed_shadow_host = driver.find_element(By.CSS_SELECTOR, "aa-dxt-qr-content")
shadow_root = driver.execute_script('return arguments[0].root.querySelector(".container-fluid")', closed_shadow_host)
input = shadow_root.find_element(By.ID, "search_input")
action.click(input).send_keys(sheet[str_getVal].value).perform()
button = shadow_root.find_element(By.ID, "search_button")
action.move_to_element(button).click().perform()

相关问题