4 回答

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以使用以下 XPath 定位元素:
driver.find_element_by_xpath('//input[@class="ng-pristine ng-valid ng-touched"][../preceding-sibling::dpm-input-label[1]/label[.="Fixed Rate"]]')
我們使用label元素作為錨點(diǎn)。獲取input滿足以下條件的元素:preceding-sibling它的第一個(gè)元素parent有一個(gè)label包含術(shù)語“固定利率”的子元素。
如果需要,添加預(yù)期條件(元素可以接收點(diǎn)擊)。假設(shè)你想在輸入框中發(fā)送“12”:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//input[@class="ng-pristine ng-valid ng-touched"][../preceding-sibling::dpm-input-label[1]/label[.="Fixed Rate"]]'))).send_keys("12")
進(jìn)口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
編輯:其他 XPath 替代品:
3 XPath 使用following-sibling軸:
//dpm-input-label[label[.="Fixed Rate"]]/following-sibling::dpm-input-number-bare[1]/input
//dpm-input-label[label[contains(.,"Fixed Rate")]]/following-sibling::dpm-input-number-bare[1]/input
//dpm-input-label[contains(.,"Fixed Rate")]/following-sibling::dpm-input-number-bare[1]/input
3 XPath 使用preceding-sibling軸和多個(gè)contains元素input:
//input[contains(@class,"ng-pristine") and contains(@class,"ng-valid") and contains(@class,"ng-touched")][../preceding-sibling::dpm-input-label[1]/label[.="Fixed Rate"]]
//input[contains(@class,"ng-pristine") and contains(@class,"ng-valid") and contains(@class,"ng-touched")][../preceding-sibling::dpm-input-label[1]/label[contains(.,"Fixed Rate")]]
//input[contains(@class,"ng-pristine") and contains(@class,"ng-valid") and contains(@class,"ng-touched")][../preceding-sibling::dpm-input-label[1][contains(.,"Fixed Rate")]]
4 XPath 使用preceding軸:
//input[@class="ng-pristine ng-valid ng-touched"][preceding::label[1][.="Fixed Rate"]]
//input[@class="ng-pristine ng-valid ng-touched"][preceding::label[1][contains(.,"Fixed Rate")]]
//input[contains(@class,"ng-pristine") and contains(@class,"ng-valid") and contains(@class,"ng-touched")][preceding::label[1][.="Fixed Rate"]]
//input[contains(@class,"ng-pristine") and contains(@class,"ng-valid") and contains(@class,"ng-touched")][preceding::label[1][contains(.,"Fixed Rate")]]

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
你試過這個(gè)嗎?
driver.find_element_by_xpath('//input[@type="text"]')
但是,如果同一個(gè) XPath 有多個(gè)元素,則可能會(huì)引發(fā)錯(cuò)誤。

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
使用 xpath 嘗試以下方法:
driver.find_element_by_xpath("//div[@class='dpm-form-row' and contains(., 'Fixed Rate')]//input[@class='ng-pristine ng-valid ng-touched']")

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
如何使用其類查找元素
driver.find_element_by_class_name('ng-pristine ng-valid ng-touched')
添加回答
舉報(bào)