【问题标题】:Javascript button wit no id not clicking with Selenium (Python)没有 id 的 Javascript 按钮不使用 Selenium (Python) 点击
【发布时间】:2020-08-26 11:33:04
【问题描述】:

我正在尝试在 Python 中使用 Selenium 单击以下按钮。但是,我尝试点击该按钮的任何方式都失败了,我希望有人能提供帮助。出于所有意图和目的。我是 Python/Selenium 的新手,因此我们将不胜感激!

<tr> 
  <td colspan=3></td>
  <td align=center valign='center'> <a href="javascript:myFunction('/mailbox/jsp/MBIList.jsp')"
      onMouseOver="chgButton('Go','GoOver'); window.status=''; return true;"
      onMouseOut="chgButton('Go','GoOff'); window.status=''; return true;"
      onMouseDown="chgButton('Go','GoDown'); return true;"><img border=0
      src="/mailbox/images/go_off.gif" vspace=7 name='Go' align='top'></a> 
  </td>
</tr>

【问题讨论】:

  • 欢迎来到 SO。请分享您尝试过的所有方法。如果您共享代码而不是猜测代码,将很容易发现问题。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

你可以使用任何元素属性点击任何元素。例如,在这种情况下,我将在您尝试选择元素的 Xpath 中使用属性 @name='Go'。

【讨论】:

    【解决方案2】:

    想要的元素是动态元素,所以点击元素你必须诱导WebDriverWaitelement_to_be_clickable(),你可以使用以下Locator Strategies之一:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='/mailbox/jsp/MBIList'] > img[name='Go'][src^='/mailbox/images/go_off']"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/mailbox/jsp/MBIList')]/img[@name='Go' and starts-with(@src, '/mailbox/images/go_off')]"))).click()
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

    • 谢谢!我收到相同的超时错误:selenium.common.exceptions.TimeoutException: Message:
    • @yms111 要解决TimeoutException,请查看thisthis 讨论。
    猜你喜欢
    • 2022-01-28
    • 1970-01-01
    • 2021-12-04
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多