无id的Javascript按钮未使用Selenium(Python)单击

2024-05-16 00:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在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>

Tags: 方法truegoreturnstatusseleniumwindow按钮
2条回答

您可以使用任何元素属性单击任何元素。例如,在本例中,我将在Xpath中使用属性@name='Go',您将在其中尝试选择元素

所需的元素是一个动态元素,因此要单击该元素,必须为element_to_be_clickable()归纳WebDriverWait,并且可以使用以下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
    

相关问题 更多 >