首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >无法使用selenium在Python中定位元素

无法使用selenium在Python中定位元素
EN

Stack Overflow用户
提问于 2020-01-10 20:41:00
回答 2查看 49关注 0票数 0

我在使用xPath定位元素时遇到了一个问题。

代码语言:javascript
代码运行次数:0
运行
复制
<li>
  <a aria-current="false" href="/drop"> 
    <button tabindex="0" type="button" style="border: 10px; box-sizing: border-box; display: inline-block; font-family: Roboto, sans-serif; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); cursor: pointer; text-decoration: none; margin: 0px; padding: 0px; outline: none; font-size: inherit; font-weight: inherit; position: relative; height: 36px; line-height: 36px; min-width: 88px; color: rgba(0, 0, 0, 0.87); transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; border-radius: 2px; user-select: none; overflow: hidden; background-color: rgba(0, 0, 0, 0); text-align: center; top: -7px;">
    <div>
      <span class="material-icons" color="rgba(0, 0, 0, 0.87)" style="color: rgba(0, 0, 0, 0.87); position: relative; font-size: 18px; display: inline-block; user-select: none; transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; vertical-align: middle; margin-left: 12px; margin-right: 0px;">
        pin_drop
      </span>
      <span style="position: relative; padding-left: 8px; padding-right: 16px; vertical-align: middle; letter-spacing: 0px; text-transform: uppercase; font-weight: 500; font-size: 14px;">Drop</span>
    </div>
    </button>
  </a>
</li>
代码语言:javascript
代码运行次数:0
运行
复制
browser.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/nav/ul/li[7]/a/button/div/span[3]').click()

嘿,我试过使用xPath和其他方法,但似乎找不到这个元素。

EN

回答 2

Stack Overflow用户

发布于 2020-01-10 21:20:11

如果您正在尝试单击Drop按钮,则使用下面的xpath。

代码语言:javascript
代码运行次数:0
运行
复制
//li/a[@href="/drop"]//span[.='Drop']

包括以下导入:

代码语言:javascript
代码运行次数:0
运行
复制
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

你的代码应该是:

代码语言:javascript
代码运行次数:0
运行
复制
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, "//li/a[@href='/drop']//span[.='Drop']"))).click()
票数 0
EN

Stack Overflow用户

发布于 2020-01-10 22:11:31

要在元素上执行click(),您必须为element_to_be_clickable()引入WebDriverWait,您可以使用以下任一Locator Strategies

使用CSS_SELECTOR

WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li > ahref=/drop > button span:nth-of-type(2)")))

  • Using WebDriverWait XPATH

WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.XPATH,"//li/a@href='/drop'/button//spantext()='Drop'")))

  • Note:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59681919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档