Reputation: 33
I am trying to click inside a text boxthat is inside a div which itself is inside a table. I have tried numerous approaches but none seems to work
This is the code for the element:
<tr valign="top"><td id="xNEPk-chdex" style="height:100%"><div id="xNEPk" class="labelRowCnt z-div"><span id="xNEPm" class="z-label">Username:</span></div></td><td id="xNEPk-chdex2" class="z-hbox-separator"><img style="height:0;width:0"></td><td id="xNEPl-chdex" style="height:100%"><div id="xNEPl" class="compRowCnt z-div"><input id="xNEPn" class="login z-textbox" autocomplete="off" value="" type="text" name="j_username"></div></td></tr>
I cannot go with the element ID as the element ID keeps changing every time I refresh the page
I have tried the following method some of which are
element =wait.until(EC.element_to_be_clickable((By.XPATH,'//div[@class=".compRowCnt.z-div"]/input[@class=".login.z-textbox"]')))
element.click()
element = driver.find_element_by_class_name("login.z-textbox")
element.click()
element =wait.until(EC.element_to_be_clickable((By.NAME,"j_username")))
element.click()
But none seems to work. Every time I get element not found.
I want to be able to click inside the textbox so that I can provide my username using send keys.
Can anyone please help me? I am using chrome
Upvotes: 1
Views: 44
Reputation: 193188
Seems you were pretty close. As the desired element is a dynamic element so to click()
the element you have 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, "input.login.z-textbox[name='j_username']"))).click()
Using XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='login z-textbox' and @name='j_username']"))).click()
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
Upvotes: 1