[英]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.