我的應用程式中有離子輸入標簽,它創建了另一個輸入標簽作為同級標簽。兄弟輸入負責任何輸入值。
我想訪問該兄弟姐妹以使用 selenium Python 輸入值(可以使用send_keys或使用javascript_executor
<ion-input data-cy="email" type="email" debounce="50" value="" class="sc-ion-input-md-h sc-ion-input-md-s md">
<input class="native-input sc-ion-input-md" autocapitalize="off" autocomplete="off"
autocorrect="off" name="ion-input-0" placeholder="" spellcheck="false" type="email"></ion-input>
每次我使用它時,data-cy="email"我只會得到 elementNotFound 例外。
我正在使用 Selenium python。
uj5u.com熱心網友回復:
根據您共享的 HTML,該元素本質上應該是唯一的,您可以檢查此 CSS 或 XPATH
路徑:
//input[@class='native-input sc-ion-input-md' and @type='email' and@name='ion-input-0']
CSS_SELECTOR:
input[class='native-input sc-ion-input-md'][type='email'][name='ion-input-0']
如果我們有獨特的條目,請檢查dev tools(谷歌瀏覽器)。HTML DOM
檢查步驟:
Press F12 in Chrome-> 轉到element部分 -> 執行CTRL F-> 然后粘貼xpath/css并查看,如果您想要element的是否使用匹配節點突出顯示。1/1
如果它們碰巧在本質上是獨一無二的,您可以嘗試使用以下代碼:
代碼1:
wait = WebDriverWait(driver, 30)
input = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@class='native-input sc-ion-input-md' and @type='email' and@name='ion-input-0']")))
input.send_keys('the string that you want to send')
代碼2:
wait = WebDriverWait(driver, 30)
input = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@class='native-input sc-ion-input-md' and @type='email' and@name='ion-input-0']")))
driver.execute_script("arguments[0].setAttribute('value', 'the string that you want to send')", input)
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
uj5u.com熱心網友回復:
我在您的代碼嘗試中沒有任何此類重大問題。但是,所需的元素是動態元素,因此理想情況下,要將字符序列發送到您需要為element_to_be_clickable()誘導WebDriverWait的元素,您可以使用以下任一Locator Strategies:
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ion-input[data-cy='email'] > input[type='email'][name^='ion-input']"))).send_keys("Sarath@N.com")使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ion-input[@data-cy='email']/input[@type='email' and starts-with(@name, 'ion-input')]"))).send_keys("Sarath@N.com")注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414623.html
標籤:
