如何使用 Python Selenium 单击下面显示的图标?

How to click the below shown icon using Python Selenium?

我附上了 Whosebug 页面的屏幕截图。

如何用Python Selenium点击它?

那是一个 SVG 元素,我确实看到 class svg-icon iconInbox 本质上是独一无二的,所以下面的 XPath 应该适合你。

//*[name()='svg' and @class='svg-icon iconInbox']

并且使用显式等待,您可以像这样执行点击:

wait = WebDriverWait(driver, 30)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @class='svg-icon iconInbox']"))).click()
    print('Clicked on the button')
except:
    print('Could not click ')
    pass

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Stack Overflow 收件箱的选择器是 "svg.iconInbox"

您可以直接从控制台验证:

document.querySelector("svg.iconInbox")

将其添加到Selenium框架中的点击方法中,您可以点击它。例如,这里是如何使用 SeleniumBase:

单击它
self.click("svg.iconInbox")

点击通知图标你需要诱导WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "svg.svg-icon.iconInbox"))).click()
    
  • 使用 XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[local-name()='svg' and @class='svg-icon iconInbox']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC