python - 使用 python 在 Selenium 中可见后单击按钮

标签 python selenium

我正在尝试使用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()

但它们似乎都不起作用。我怎样才能克服这种情况并最终点击这个按钮?

enter image description here

最佳答案

尝试以下代码:

使用 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/

相关文章:

python - 如何在 GAN 中平衡生成器和鉴别器的性能?

java - 从 beforeInvocation 方法停止 Testng 测试

html - 使用 Selenium 检查音频是否正确播放

selenium - Chrome 在 linux 服务器上以 headless 模式工作,但通过 Selenium 和 Python 在 gui 模式下显示 "DevToolsActivePort file doesn' t exist"error

java - PhantomJS 驱动程序无法启动

python - 从无服务器文件夹外部导入自制的 Python 模块

python - 在输出文件 [Python] 中写入超过 80 个字符的行

python - Netsuite REST Web 服务 API GET 请求,在 Postman 之外

python - 是否可以使用 Matplotlib 绘制隐式方程?

python-3.x - 在 pip3 中安装 Selenium 时出错