我在使用xPath定位元素时遇到了一个问题。
<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>
browser.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[1]/nav/ul/li[7]/a/button/div/span[3]').click()
嘿,我试过使用xPath和其他方法,但似乎找不到这个元素。
发布于 2020-01-10 13:20:11
如果您正在尝试单击Drop按钮,则使用下面的xpath。
//li/a[@href="/drop"]//span[.='Drop']
包括以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
你的代码应该是:
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, "//li/a[@href='/drop']//span[.='Drop']"))).click()
发布于 2020-01-10 14: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)")))
XPATH
:WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.XPATH,"//li/a@href='/drop'/button//spantext()='Drop'")))
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
https://stackoverflow.com/questions/59681919
复制相似问题