简体   繁体   English

如何在Selenium Webdriver python3.7中使用带有内部html的find_element_by_xpath单击按钮

[英]how to click button using find_element_by_xpath with inner html in selenium webdriver python3.7

first_click_content = driver.find_element_by_xpath("//div[@class='recent-report-wrapper']/div[2]/div/div[6]/div[2]")

print(first_click_content.get_attribute('innerHTML')

The above code give the result like this: 上面的代码给出如下结果:

<button class="buttonWhite js-report-rerun">Re-run</button>
<button class="buttonWhite marginLeft js-report-edit">Edit</button>
<button class="buttonWhite marginLeft js-report-remove">Remove</button>
<button class="buttonWhite marginLeft js-report-save" style="display: none;">Save </button>
<button class="buttonWhite marginLeft js-report-view-errors" style="display: none;">View Errors</button>
<button class="buttonReportGreen marginLeft js-report-view" style="display: none;">View</button>
<button class="buttonReportGreen marginLeft js-report-download" style="display: inline-block;">Download</button>

I want to click the first button, how can I do that? 我要单击第一个按钮,该怎么做?

As the all the buttons are JavaScript enabled element you need to induce WebDriverwait for the desired element to be clickable and you can use either of the following solutions: 由于所有按钮都是启用JavaScript的元素,因此您需要诱使WebDriverwait使所需的元素可单击,并且可以使用以下解决方案之一:

  • CSS_SELECTOR : CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.buttonWhite.js-report-rerun"))).click() 
  • XPATH : XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='buttonWhite js-report-rerun' and contains(.,'Re-run')]"))).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.

相关问题 如何使用find_element_by_xpath或selenium-python API从以下HTML代码中选择“开始”按钮并单击它 - How to select 'Start' button and click it from following HTML code by using find_element_by_xpath or selenium-python API 无法在 Python Selenium webdriver find_element_by_xpath 中找到元素 - Unable to locate element in Python Selenium webdriver find_element_by_xpath Web上的Selenium WebDriver“find_element_by_xpath” - Selenium WebDriver “find_element_by_xpath” on WebElement WebElement 上的 Selenium WebDriver “find_element_by_xpath” - Selenium WebDriver “find_element_by_xpath” on WebElement 使用“find_element_by_xpath(xpath)”的 Python Selenium 问题 - Issue with Python Selenium using `find_element_by_xpath(xpath)` python selenium:无法使用find_element_by_xpath或id单击树节点元素 - python selenium: Not able to click in a tree node element using find_element_by_xpath or id 使用 find_element_by_xpath 查找带有 selenium 的“添加到购物车”按钮 - Finding Add To Cart button with selenium using find_element_by_xpath Python Selenium 上的错误(find_element_by_xpath) - Error on Python Selenium (find_element_by_xpath) find_element_by_xpath() 使用 Selenium 和 Python 显示语法错误 - find_element_by_xpath() shows syntax error using Selenium with Python Selenium和Webdriver遍历find_element_by_xpath()的元素列表 - Loop over list of elements for find_element_by_xpath() by Selenium and Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM