Selenium:无法在影子根 WebElement 上使用 click()。

huangapple go评论83阅读模式
英文:

Selenium: cannot use click() on shadow root WebElement

问题

driver.get("https://colp.maps.arcgis.com/apps/dashboards/0334150e430449cf8ac917e347897d46")
driver.implicitly_wait(15)

cyber_button_outer = driver.find_element(By.XPATH, "//span[@title='Fraud or Cyber Crimes']/ancestor::div[contains(@class, 'category-selector-widget h-full')]").find_elements(By.CLASS_NAME, "group-btn")     

for button in cyber_button_outer:
    shadow_root = driver.execute_script("return arguments[0].shadowRoot", button)
    try:
        check_button = shadow_root.find_element(By.CSS_SELECTOR, "button")
        print(check_button.text)
        if check_button.text == "Cyber Crime":
            print("1")
            check_button.click()
            
    except:
        print("false")
英文:
driver.get("https://colp.maps.arcgis.com/apps/dashboards/0334150e430449cf8ac917e347897d46")
driver.implicitly_wait(15)

cyber_button_outer = driver.find_element(By.XPATH, "//span[@title='Fraud or Cyber Crimes']/ancestor::div[contains(@class, 'category-selector-widget h-full')]").find_elements(By.CLASS_NAME, "group-btn")     

for button in cyber_button_outer:
    shadow_root = driver.execute_script("return arguments[0].shadowRoot", button)
    try:
        check_button = shadow_root.find_element(By.CSS_SELECTOR, "button")
        print(check_button.text)
        if check_button.text == "Cyber Crime":
            print("1")
            check_button.click()
            
    except:
        print("false")

I would like to click on the button that was hidden inside the shadow root in this website (https://colp.maps.arcgis.com/apps/dashboards/0334150e430449cf8ac917e347897d46). I think I successfully called out the web element but I just wasn't able to click on it with click().

Thanks for any help in advance Selenium:无法在影子根 WebElement 上使用 click()。

screenshot for the button

Selenium:无法在影子根 WebElement 上使用 click()。

答案1

得分: 1

要使用Python中的Selenium与Shadow DOM元素进行交互,您可以使用JavaScript来访问阴影根并执行所需的操作。

from selenium import webdriver
from selenium.webdriver.common.by import By

# 启动新的浏览器会话
driver = webdriver.Chrome()

# 导航到网页
driver.get("https://colp.maps.arcgis.com/apps/dashboards/0334150e430449cf8ac917e347897d46")
driver.implicitly_wait(15)

# 查找阴影根的外部元素
cyber_button_outer = driver.find_element(By.XPATH, "//span[@title='Fraud or Cyber Crimes']/ancestor::div[contains(@class, 'category-selector-widget h-full')]").find_element(By.CLASS_NAME, "group-btn")

# 使用JavaScript访问阴影根
shadow_root = driver.execute_script("return arguments[0].shadowRoot", cyber_button_outer)

try:
    # 在阴影根中查找所需的按钮
    check_button = shadow_root.find_element(By.CSS_SELECTOR, "button")
    print(check_button.text)
    if check_button.text == "Cyber Crime":
        print("1")
        # 使用JavaScript执行点击操作
        driver.execute_script("arguments[0].click()", check_button)
except:
    print("false")

# 关闭浏览器
driver.quit()

与Shadow DOM元素进行交互可能会比较棘手,具体取决于页面结构。如果可能的话,考虑要求开发人员将元素在常规DOM中可访问,以简化自动化操作。

英文:

To interact with Shadow DOM elements using Selenium in Python, you can use JavaScript to access the shadow root and perform the desired actions.

from selenium import webdriver
from selenium.webdriver.common.by import By

# Start a new browser session
driver = webdriver.Chrome()

# Navigate to the webpage
driver.get("https://colp.maps.arcgis.com/apps/dashboards/0334150e430449cf8ac917e347897d46")
driver.implicitly_wait(15)

# Find the outer element of the shadow root
cyber_button_outer = driver.find_element(By.XPATH, "//span[@title='Fraud or Cyber Crimes']/ancestor::div[contains(@class, 'category-selector-widget h-full')]").find_element(By.CLASS_NAME, "group-btn")

# Access the shadow root using JavaScript
shadow_root = driver.execute_script("return arguments[0].shadowRoot", cyber_button_outer)

try:
    # Find the desired button within the shadow root
    check_button = shadow_root.find_element(By.CSS_SELECTOR, "button")
    print(check_button.text)
    if check_button.text == "Cyber Crime":
        print("1")
        # Perform the click action using JavaScript
        driver.execute_script("arguments[0].click()", check_button)
except:
    print("false")

# Close the browser
driver.quit()

Interacting with Shadow DOM elements can be tricky, and it depends on the specific page structure. If possible, consider asking the developers to make elements accessible in the regular DOM to simplify automation.

huangapple
  • 本文由 发表于 2023年7月20日 18:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定