4 回答

TA貢獻1808條經驗 獲得超4個贊
您可以使用以下 XPath 定位元素:
driver.find_element_by_xpath('//input[@class="ng-pristine ng-valid ng-touched"][../preceding-sibling::dpm-input-label[1]/label[.="Fixed Rate"]]')
我們使用label元素作為錨點。獲取input滿足以下條件的元素:preceding-sibling它的第一個元素parent有一個label包含術語“固定利率”的子元素。
如果需要,添加預期條件(元素可以接收點擊)。假設你想在輸入框中發送“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")
進口:
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軸和多個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貢獻2051條經驗 獲得超10個贊
你試過這個嗎?
driver.find_element_by_xpath('//input[@type="text"]')
但是,如果同一個 XPath 有多個元素,則可能會引發錯誤。

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

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