I have a page that can have several datepickers on a page.
I run the first loop find out how many datepickers the are.
List<WebElement> calanders = driver.findElements(By.cssSelector(".c-input-group__addon"));
for(int i1=0; i1<calanders.size(); i1++) {
System.out.println("how many calanders " +i1);
}
I then loop through the datepicker and click on the required date.
I then need to click on a button to close the picker to continue, this is where i am having an issue.
calanders.get(1).findElements(By.xpath("//label[contains(#class,'c-option')]")).click;
How can i click on the second occurence of By.xpath("//label[contains(#class,'c-option')] ??
List<WebElement> calanders1 = driver.findElements(By.cssSelector(".c-input-group__addon"));
for(int i1=1; i1<calanders1.size(); i1++) {
driver.findElements(By.cssSelector(".c-input-group__addon"));
calanders1.get(i1).findElement(By.xpath("//label[contains(#class,'c-option')]")).click();
}
Try to find via F12 how many elements are on the page by your xpath.
Maybe it couldn't click because there are more elements than 1 by your locator.
You could add waiter until element is clickable.
Or just add Thread.sleep() but it isn't good practice.
p.s. It is better to print exception log to your question
Related
I am new to selenium webdriver in Java. I am using webdriver with Firefox Quantum (59.0, x64). The problem I am facing is that the website i am writing code for testing dose not have constant xpath. On every visit the button id changes. Even the classname(launchbutton) is same for all 4 Launch Buttons. So while writing code it initially worked & opened the first link, but for second link it again opened the first link as the classname is same for all 4 buttons. Please help me out to code to open other links too by pressing other launch button.
i used this(below) code but it worked partially. Please help me out as i want to launch using button of other subjects too. (Please refer screenshot)checkout screenshot1 here
checkout screenshot2 here
Also i am not able to switch between tabs. pls help.
thankyou
driver.findElement(By.className("launchbutton")).click();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.get("http://google.com");
//Grab all elements which have the className "launchbutton"
List<WebElement> elements = driver.findElements(By.className("launchbutton"));
//Iterate your list of matching elements and look at the innerText for the button you want
//When found, click it and break the for loop
foreach(WebElement ele in elements)
{
if(ele.getAttribute("innerText") == "The button text you want")
{
ele.Click();
break;
}
}
You can click the Launch button as below and please change the code based on the mentioned comments
//Specify the Expected Name in the below String
String subjectName="";
//Find the Table Element using any one of the Unique Locator
WebElement table=driver.findElement(By.id(""));
List<WebElement> rowElementList=table.findElements(By.xpath(".//tr"));
//I have assumed Launch button column in 5th.So, Specified as td[4].
//Please cross check the Launch button column and change the index accordingly
for(WebElement element:rowElementList){
if(element.findElement(By.xpath("./td")).getText().equalsIgnoreCase(subjectName)){
element.findElement(By.xpath("./td[4]/a")).click();
}
}
I'm having trouble with this particular link because is dynamic, it doesn't have an specefic name or id, but i know that all those links are inside a span with has a class. I try to get a list of WebElements to get all the spans with the class "more" to get the bunch of links inside but i get this error:
org.openqa.selenium.WebDriverException: Element is not clickable at point (96, 21). Other element would receive the click: <div class="priceFinderHeaderLogoWrap"></div>
this how the html code looks likes:
<span class="more">
<a onclick="setPID(27272);ta.fireEvent('hotels_lists_engagement_tracking.fired', {type: 'ReviewCount', element: this});ta.setEvtCookie('Reviews', 'ReviewCount', '1461750', 1, '/Hotel_Review');" target="_blank" href="/Hotel_Review-g150800-d1461750-Reviews-City_Express_Plus_Reforma_El_Angel-Mexico_City_Central_Mexico_and_Gulf_Coast.html#REVIEWS">254 opiniones</a></span>
my java code:
List<WebElement> links = driver.findElements(By.className("more"));
System.out.println(links.isEmpty());
for (int i = 0; i < links.size(); i++) {
links.get(i).click();
// do something in the web page...
}
Try this
List<WebElement> links = driver.findElements(By.XPath("//span[#class='more']/a"));
As per provided information, links are dynamic which is inside span with class as more. So in the code you tried
List<WebElement> links = driver.findElements(By.className("more"));
asking to load all span webelements but actually you need links right?
So if you need to load all links in particular span with class more then try like below
List<WebElement> links = driver.findElements(By.xpath("//span[#class='more']/a"));
provided that page contains unique span which you trying to get links.Then go for 'for' loop and click on link.
if you still get same issue, then use Actions to click on element. for example like below
for(int i=0; i<links.size(); i++){
new Actions(driver).moveToElement(links.get(i)).click().build().perform();
}
if your intention is to click on span as per code you tried then try Actions as above to get out of the exception.
Please make a note that there is chance of getting stealelement exception as you are performing click which may leads to page loads and driver loose its references to collected webelements in for loop.
Thank You,
Murali
Below is the site,
http://www.mortgagecalculator.org/
click on Get Today's Best Mortgage Rates & you will get one popup window,which
is there in frame. select purchase radio button and click on search button.
Here is my code. I am unable to select the radio button with my code. need suggestion. Thanks
driver.get("http://www.mortgagecalculator.org/");
driver.findElement(By.xpath(".//*[#id='calc']/form/section/section[2]/div/div/div[1]/div/div/div[3]/div[1]/div[1]/div[4]/a/strong/font")).click();
Thread.sleep(3000);
driver.switchTo().frame("brbodxlxcs");
Thread.sleep(4000);
System.out.println("***");
driver.findElement(By.xpath(".//*[#id='brTabbedRateTable']/div[1]/form[2]/div/div[3]/div[1]/ul/li[1]/input")).click();
driver.findElement(By.xpath(".//*[#id='brTabbedRateTable']/div[1]/form[2]/div/div[3]/div[2]/ul/li[8]/a")).click();
Thread.sleep(2000);
There is browser security related to iframes.
You cannot get access to the elements in an iframe like you can other parts of your page.
Others have struggled with this also:
Selecting an element in iFrame jQuery
Change driver.switchTo().frame("brbodxlxcs"); to match something like driver.switchTo().frame(driver.findElement(By.tagName("iframe[title='Fill Quote']")));
The frame that you are referring to brbodxlxcs , seems like id of this frame is dynamically changing.
Try this, its working for me
driver.get("http://www.mortgagecalculator.org/");
driver.findElement(By.xpath(".//*[#id='calc']/form/section/section[2]/div/div/div[1]/div/div/div[3]/div[1]/div[1]/div[4]/a/strong/font")).click();
Thread.sleep(8000);
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
System.out.println(frames.size());
Iterator<WebElement> it = frames.iterator();
while(it.hasNext())
{
WebElement temp = it.next();
System.out.println(temp.getAttribute("id"));
}
This gives me all the available frames, in this case its 5.
Then i tried for different frames available one by one & finally got this to work by
driver.switchTo().frame(2);
also i modified xpath for radio button like
.//*[#id='brTabbedRateTable']//form[#name='mtgSearchForm']//input[#name='loantype' and #value='purchase']
& Search button like this,
.//*[#id='brTabbedRateTable']//form[#name='mtgSearchForm']//a[text()='Search' and #class='br-submit']
I hope it helps !!
It looks the problem you are having is that the iFrame id is dynamic and is different every time the page is rendered. There are two iFrames on the page so use an xpath like to one below to find a specific instance:
(//iframe)[2]
Currently, I am doing some Selenium stuff. While doing so, I got stuck in one of the Xpath, which is for Gmail account creation page that is for the birth- month selection option. I have given the Xpath as below which looks good in Firepath as well as in console.
.//span[#id='BirthMonth']//div[2]//div//div
However, in Java code it's not working; instead, the program is getting hung.
Jave code below is for the above Xpath. Kindly anyone suggest me the right Xpath. Or please let me know if there is anything wrong in my code or Xpath.
List<WebElement> gElements = Driver.findElements(By.xpath(".//span[#id='BirthMonth']//div[2]//div//div"));
You are trying to click div which has display:none property.See code below:
driver.findElement(By.xpath("//span[#id='BirthMonth']/div[#title='Birthday']")).click();
driver.findElement(By.xpath("//span[#id='BirthMonth']/div[2]/div[1]/div")).click();
When first div inside span is clicked then div[2] and all its children are displayed.
Use below lines will display the list of all birth months:
driver.findElement(By.xpath("//span[#id='BirthMonth']/div")).click();
List<WebElement> listOfMonths= driver.findElements(By
.xpath("//div[#class='goog-menu goog-menu-vertical']"));
System.out.println("Total months: " + listOfMonths.size());
for (int i = 0; i < listOfMonths.size(); i++) {
System.out.println("MonthName: " + listOfMonths.get(i).getText());
}
<a onclick="requestReportGeneration('857f23e1baa767622a91f970963d8918', 'reportDiv31','CSV')" href="javascript:void[0];">CSV</a>
<a onclick="requestReportGeneration('64107e36323e5877c986edc98a17b6e8', 'reportDiv32','CSV')" href="javascript:void[0];">CSV</a>
<a onclick="requestReportGeneration('2cad4d4e5c8855c47a88b6ddf8345735', 'reportDiv33','CSV')" href="javascript:void[0];">CSV</a>
I have these three links on a page and I want to click each one in turn. I am reading all the links on the page into a list of WebElements and then I go through each one in turn if the href contains javascript:void[0] I then try to click it:
for (int i = 0; i < allLinks.size(); i++) {
String reportLink = allLinks.get(i).getAttribute("href");
if (reportLink.contains("javascript:void[0];"))
{
allLinks.get(i).click();
/// Do some more stuff
}
The problem is I keep getting an error saying the element is not visible. I have also tried just loading the page and instead of getting all the links doing
driver.findElement(By.xpath("//a[contains(#href,\"javascript:void[0]\")]")).click();
but that also just gives element not visible error.
Can anybody tell me why this isn't working?
Try putting a breakpoint before the while loop, debug your script, then step into it and use Eclipse's Display tab to interrogate what is up with the links. Try evaluating sample statements like:
allLinks.size();
allLinks.get(i).isDisplayed();
allLinks.get(i).isEnabled();
There must be something odd about the links (or the way webdriver is seeing them), but debugging like this will let you find out what it is.