简体   繁体   中英

Selenium adds hash to findElement

I'm writting a code in Selenium Webdriver. I have to click the button with HTML code is

<input id="aui_3_4_0_1_300" class="aui-button-input aui-button-input-submit" type="submit">

My code then is as simply as following

driver.findElement(By.id("aui_3_4_0_1_300")).click();

But when I'm running a code (using Maven), I got an information

Unable to locate element: #aui_3_4_0_1_300

I got also this problem with

driver.findElement(By.id("_58_login")).clear();

and also got the Unable to locate element: #_58_login , but I thought it was a problem with underscores. (I solved it by adding synchronized(driver) {driver.wait(1000);} before that line and it works).

Why compiler added the hash # ?

Compiler adds nothing to your selector- "#" just stands for id in CSS as well as "." stands for class name.

So By.id("aui_3_4_0_1_300") is the same as By.cssSelector("#aui_3_4_0_1_300") and

By.className("aui-button-input") is the same as By.cssSelector(".aui-button-input")

It seems that there is a timing issue- required element generated dynamically, so you need to wait until it present in DOM :

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(elementToBeClickable(By.id("aui_3_4_0_1_300"))).click();

If id value "aui_3_4_0_1_300" is not constant you might need to match element by patrial id value (starting characters):

wait.until(elementToBeClickable(By.cssSelector("input[id^='aui_']"))).click();

or just use another elements' attribute:

wait.until(elementToBeClickable(By.cssSelector("input[type='submit']"))).click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM