首页 > 解决方案 > 使用 Python 和 selenium webdriver 单击没有 id/name 的按钮元素

问题描述

我正在使用 Python 和 selenium 扫描网页,我想单击一个如下所示的按钮:

稍后>

定义为:

<div class=”later___16eg8 col-xs-3of12”&gt;
  <button class=”visible-xs-inline visible-sm-inline visible-md-inline bdp___2gfNX textButton___ETrkF” data-test=”later-small-button”&gt; == $0
     “Later”
    <span aria-hidden=”true”&gt;></span>
  </button>
  <button class=”visible-lg-inline visible-xl-inline bdp___2gfNX textButton___ETrkF” data-test=”later-large-button”&gt; 
     “Later”
    <span aria-hidden=”true”&gt;></span>
  </button>
</div>

我试过了:

later = driver.find_element_by_xpath('//button[text()="Later"]')
later.click()

当第一行找到按钮(或其一部分)时,第二行会生成以下错误:selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

我也试过:

later = driver.find_element_by_xpath('//button[text()="Later>"]')

这产生了以下错误:

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//button[text()="Later>"]"}

有任何想法吗?

谢谢

标签: pythonselenium

解决方案


你试过 //div[@class='later___16eg8 col-xs-3of12']/button/button[text()='Later']


推荐阅读