简体   繁体   English

Python selenium单击没有id的按钮

[英]Python selenium click a button without id

i have this code我有这个代码

<a href="#" class="button expanded vote" style="background: rgb(51, 204, 102) none repeat scroll 0% 0%; border-radius: 5px;" onclick="Poll.sendAnswer("Programn2015","Answer1","Answer2")">Vote</a>

what could i do?我能做什么? there are other buttons on the page with the same class etc, the only variables are页面上还有其他按钮具有相同的类等,唯一的变量是

"Answer1","Answer2"

To click on the link with text as Vote you can use either of the following solutions:要单击带有文本作为投票的链接,您可以使用以下任一解决方案:

  • css_selector : css_selector :

     driver.find_element_by_css_selector("a.button.expanded.vote[onclick*='Answer1'][onclick*='Answer2']").click()
  • xpath : xpath

     driver.find_element_by_xpath("//a[@class='button expanded vote' and contains(.,'Vote')][contains(@onclick,'Answer1') and contains(@onclick,'Answer2')]").click()

Update更新

Induce WebDriverWait for the desired element to be clickable as follows:诱导WebDriverWait使所需元素可点击,如下所示:

  • css_selector : css_selector :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.expanded.vote[onclick*='Answer1'][onclick*='Answer2']"))).click()
  • xpath : xpath

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button expanded vote' and contains(.,'Vote')][contains(@onclick,'Answer1') and contains(@onclick,'Answer2')]"))).click()

Note : You have to add the following imports :注意:您必须添加以下导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM