我正在尝试使用Python和Selenium模拟搜索,但最近我遇到了一个问题,当生成一个按钮但生成一个JS时。在这里,在下图中,我尝试单击的按钮是“保存...”,并且在搜索完成后可见(并且可单击)。使用Firefox中的代码分析器,我发现这个按钮的代码是:
<div class="pharmit_minbottom">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button">
<span class="ui-button-text-only">
Save...
</span>
</button>
</div>
但是在此代码中还有两个具有相同文本标签的按钮:
<div class="pharmit_resfooter">
<div class="pharmit_bottomloaders pharmit_nowrap">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-disabled ui-state-disabled ui-button-text-only" disabled="" role="button">
<span class="ui-button-text">
Minimize
</span>
</button>
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-disabled ui-state-disabled ui-button-text-only" disabled="" role="button">
<span class="ui-button-text">
Save...
</span>
</button>
</div>
</div>
我尝试使用两段不同的代码来单击它:
time.sleep(30) #wait for the search to finish
driver.find_element_by_class_name('ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only').click()
还有一个:
wait = WebDriverWait(driver, 30)
wait = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only')))
wait.click()
但它们似乎都不起作用。我怎样才能克服这种情况并最终点击这个按钮?
最佳答案
尝试以下代码:
使用 WebDriverWait:(有助于避免加载缓慢时的 sleep )
wait = WebDriverWait(driver, 30)
saveButton = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='pharmit_bottomloaders pharmit_nowrap']/button[2]/span")))
saveButton.click()
没有 WebDriverWait:
driver.find_element_by_xpath("//div[@class='pharmit_bottomloaders pharmit_nowrap']/button[2]/span").click()
关于python - 使用 python 在 Selenium 中可见后单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41522713/