Is there a trick to selecting page elements one after the other using java webdriver? I am trying to set values in a form which is inside an iframe.
I first switch the iframe and can access and change the values in the first dropdown. I can create a WebElement from the other pages but I cannot interact with them at all.
The code below allows me to change the value of the first drop down.
driver.switchTo().frame(0)
WebElement fromList = driver.findElement(By.id("foo"));
r.selectItemByText(fromList, "var");
I cannot access any of the elements after that. I can find them all with no error but I cant do anything with them as it says they have no data.
WebElement fromList = driver.findElement(By.id("bar"));
r.selectItemByText(fromList, "foo");**
The second line returns this error:
NoSuchElementException: Cannot locate element with value 'foo'
If I remove it I get no error so the code is seeing the element just not the values of it.
If the elements are on a different frame, you must switch back to the default frame, then back down again.
Something like:
driver.switchTo().defaultContent();
Reason is because once you switch to a frame, if you switch to another frame, the search will be only of the child frames to that original frame. So you must switch back to the top frame, and back down again.
Related
Aim: To count links in footer section of a webpage.
Instead of this:
WebElement footerdriver=driver.findElement(By.id("gf-BIG"));
system.out.println(footerdriver.findElement(By.tagName("a")).size());
I want to write like this:
WebElement footerdriver=driver.findElement(By.id("gf-BIG"));
driver.switch(footerdriver);
system.out.println(driver.findElement(By.tagName("a")).size());
Is this correct way to use "switch()" method in selenium ?
Unless I am mistaken, switch() is not a method belonging to the Selenium Webdriver. The method switchTo() is used to change your focus to a different window, tab or iframe.
If I am understanding your question you want to find an element that is descending from a different element. The way to do this is just find the element using the WebElement method findElement(By locator). E.g:
driver.get("https://www.wikipedia.org/");
WebElement eng = driver.findElement(By.xpath("//a[#id='js-link-box-en']"));
System.out.println(eng.findElement(By.tagName("span")).getText());
This will find a span element that descends from the element "eng".
Here is a runnable example of a test using the method.
driver.switchTo();
should only be used to either switch to
iframes/frame/frameset
newly windows/tabs
Alerts
defaultContents
therefore,
WebElement footerdriver=driver.findElement(By.id("gf-BIG"));
driver.switch(footerdriver);
this is wrong since you are saying that you want to switch to a web element.
Also in selenium it is switchTo() not switch.
I need to click some element that is inside an iframe. When page is loaded these elements are not visible and I scroll down to make them visible. I tried as follows:
Move to iframe and switchTo(frame)
javascript.executeScript(argument[0].scrollIntoView(true), webelement)
Scrolling is happening only once. When I click first element. I need to scroll down and click the next element (it is a submission form in ui). All elements are in iframe.
Can anyone suggest any idea I'am using java with selenium.
Try this:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", myIWebElement);
Trying to click on service by using xpath
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/header/section/div[2]/div/div/ul/li[2]/a")));
driver.findElement(By.xpath("/html/body/header/section/div[2]/div/div/ul/li[2]/a")).click();
but element is not getting selected/clicked.please help
try as follows (if the element is not inside a frame):
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#id='menu1']/span[text()='Service']")));
driver.findElement(By.xpath("//a[#id='menu1']/span[text()='Service']")).click();
If above code does not work. then it is most probably the element is inside a frame.
If the element is inside a frame (the element is a child of an iframe tag), then first switch to the element and find the element.
My detailed answer about switching b/w frames here,
in selenium web driver how to choose the correct iframe
driver.findElement(By.xpath("//*[#id='menu1']"))
try to use this. else
driver.findElement(By.id("menu1"))
I am trying to navigate through all the available pages in a website, at the beginning after scrolling to the second page I was getting exception that there is no such element on the web page, then I realize, at some point the css selector is changed in the website. Sometimes it is like that WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head>a>div>span.hidden-xs.hidden-sm")); and sometimes WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head > a:nth-child(3) > div > span.hidden-xs.hidden-sm")); My question is how I can handle this in selenium, I checked whether there is a method to check somehow whether a given webelement exist or not but I could no find any :/
You could use this cssSelector for both paths:
WebElement nextButton = driver.findElement(By.cssSelector("#pagination-head span.hidden-xs.hidden-sm"));
If its only these 2 options, you could work with a workaround if/else statement.
You could create a boolean checking if element can be found by your first findElement. If true, use your first code, else, use second code.
You can use if-else block if you are sure that the next button is identified only by those two locators. If in case on the third page the locator changes, your if-else block will not work. So, better approach is to go for xpath to identify the next button. You can use below sample code to locate your element
WebElement nextButton = driver.findElement(By.xpath(".//*[contains(text(),'(text on your next button)')]"));
nextButton.click();
Please replace "(text on your next button)" with the text displayed on your next button. The above code will perform the click operation on the element containing the text which you pass.
Hope this is helpful.
I am automating a code using selenium 2.0. I select one (or several) user(s) from a list. Then I click on an add button which makes the user name(s) visible on a grid. Each user will have a valid Xpath when visible on the grid. However, even after erasing all user names from the grid which actually disappears if there is no user names displayed, the Xpath still does not return null. I am using Xpath to check if it returns null when the object (user name) is not visible, but it does not work as expected. Is there any other way to solve my problem? I am pretty new with Selenium. I am using selenium 2.0. Bellow is a section of my code. Your help will be very appreciated.
//Check if user is present on the grid
By checkuser = By.xpath( ".//*[#id='sharing_list']/tbody/tr/td[1]/span");
//if the grid is not empty, which means the grid is visible...
if(null!=checkuser) //where the problem is!!
{
//Click the button to erase the names in the grid, then the grid desapears
webDriver.findElement(By.xpath("//*[#id='sharing_list']/tbody/tr/td[4]/span")).click();
Thread.sleep(2000);
//more code
//............
}
I can see two things happening here:
First is that your XPath is generic enough that it is selecting some other element that isn't a user. To see if this is the case, then in Chrome, go to the page and do the necessary actions to get it in the state you want. Next, press Ctrl-Shift-J, click on Console, and type in $x("//*[#id='sharing_list']/tbody/tr/td[4]/span"). Chrome will then show you which element your selector is selecting.
Your task then, is to identify if its selecting some other element, or whether the element is just not visible. It is definitely possible to have an element on a page, but not visible, and WebDriver WILL select invisible elements (unless you are doing By.linkText()). If you want to check to see if an element is visible do a element.isDisplayed().