I have ion-input tags in my app and that creates another input tag as a sibling. The sibling input is responsible for any input value.
I want to access that sibling to enter a value using selenium Python (can be using send_keys or using javascript_executor
<ion-input data-cy="email" type="email" debounce="50" value="" >
<input autocapitalize="off" autocomplete="off"
autocorrect="off" name="ion-input-0" placeholder="" spellcheck="false" type="email"></ion-input>
Everytime I use that data-cy="email" I am getting elementNotFound exception only.
I am using Selenium python.
CodePudding user response:
The element should be unique in nature, based on the HTML that you've shared, can you check this CSS or XPATH
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']
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath/css and see, if your desired element is getting highlighted with 1/1 matching node.
If they happen to be unique in nature you can try with the below code:
Code1:
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')
Code2:
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)
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
CodePudding user response:
I don't any such major issues in your code attempts. However the the desired element is a dynamic element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using 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("[email protected]")Using 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("[email protected]")Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
