【问题标题】:How to click on the "Mail" button on the yahoo page using Selenium and Python如何使用 Selenium 和 Python 点击​​雅虎页面上的“邮件”按钮
【发布时间】:2020-07-04 18:40:48
【问题描述】:

我为这些按钮设置了一个 BUTTON 变量:

GoMailsBTN = browser.find_element_by_class_name("D(ib) Fz(14px) Fw(b) Lh(24px) Pstart(38px)")
GoMailsBTN.click()

而且没有任何id,如果你愿意,你可以自己去看看! 这是弹出的错误 selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定了无效或非法的选择器 有人知道这是为什么吗?

按钮截图:

【问题讨论】:

  • 在您的答案中发布按钮的 HTML,以便我们创建定位器。 ...问题是您传递的字符串不是有效的类名。有效的类名中没有空格......而且可能没有括号。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

类名是动态的,可以改变,这种情况下可以使用xpath:

browser.find_element_by_xpath("//li//a[contains(@href ,'mail') and not(@id)]")

【讨论】:

    【解决方案2】:

    所需的元素是启用了JavaScript 的元素,因此要单击该元素,您必须为WebDriverWait 诱导element_to_be_clickable(),您可以使用以下Locator Strategies 之一:

    • 使用CSS_SELECTOR

      driver.get('https://in.yahoo.com/?p=us')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#header-mail-button span"))).click()
      
    • 使用XPATH:

      driver.get('https://in.yahoo.com/?p=us')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='header-mail-button']//span"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2021-03-06
      • 2021-10-07
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多