Monty
Monty

Reputation: 5

Selenium WebDriver findElement(By.xpath()) not working for my case

I am trying to identify the element in IE using below xpath values in selenium webdriver but it is not working for any of them

driver.findElement(By.xpath("//input[@id='userName']"))
driver.findElement(By.xpath("//*[@id='userName']"))

Also tried complete xpath but did not work -

//frameset[contains(@id,'topFrameset')]/frame[4]/html/body/table/tbody/tr[1]/td/table/form/tbody/tr[5]/td/table/tbody/tr[1]/td/input

following is the input tag on the page which I am trying to access (there are no duplicate id, or name or class on the page)

<input name="userName" class="LoginTextField" id="userName" 
    onkeyup="OnKeyDownOnObject()" type="text" valign="middle"/>

Note that this input tag is inside iframe as per below entire path

//frameset/frame[4]/html/body/table/tbody/tr[1]/td/table/form/tbody/tr[5]/td/table/tbody/tr[1]/td/input

Please let me know if there is standard way in webdriver to capture elements in iframe.

Upvotes: 0

Views: 2246

Answers (1)

Subh
Subh

Reputation: 4424

As I see, your concerned frame is the 4th one under the frameset tag, you can first switch to it and then try locating the element.

Please see if the below code works out for you:

//To switch to the required frame, if found, in 20 seconds
try{
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frameset[contains(@id,'topFrameset')]/frame[4]")));
}catch(Throwable e){
    System.err.println("Error while switching to the frame. "+e.getMessage());
}

//locating the input element
driver.findElement(By.xpath("//input[@id='userName']"));

Upvotes: 1

Related Questions