我正在尝试使用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()
但它们似乎都不起作用。我怎样才能克服这种情况,最后点击这个按钮?
答案 0 :(得分:0)
尝试以下代码:
使用WebDriverWait:(有助于在加载缓慢的情况下避免睡眠)
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()