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());
}
Related
I am building a java bot using selenium in order to automate a process and I ran into some trouble. There is 5 drop down menus in which I need to select "select all" however the "select all" button has the same xpath and classname for each drop down so the system is not recognizing that. Ive attached a picture so you can see the drop downs. Please if you know how I can select these elements that would be great.
The xpath for the select all exists as = "//*[#id=\"capability-filter\"]/div[2]/div[1]"
You can try using indexing here if they all have same xpath then use:
(//*[#id="capability-filter"]/div[2]/div1)[1]
this will select the first element from this list.
As you said there are 5 dropdown menu so in the for-loop specified size is 5
for(int i = 1 ; i<=5 ; i++){
driver.findElement(By.xpath((("//*[#id=\'capability-filter']/div[2]/div)["+i+"]")))).click();
}
Hope above code will help you
You can use the following code:
List<WebElement> ele = driver.findElements(By.xpath("//*[#id=\"capability-filter\"]/div[2]/div[1]"));
for(WebElement e: ele){
String drop = e.getText.toString();
if(drop.equalsIgnoreCase("Select All")){
e.click();
break;
}
My question is how do i click the radio button of html web table?
Additional info:
My code works just fine however I cant click the radio button of selected column/row using xpath. The xpath I am using within the loop is not working. However the xpaths I am using work individually in the Chrome browser. I am thinking feel I need to join both together but that did not work probably because i am not an expert in using xpath. I have been working on this for the past 24 hours and no solution. I have search for a solution on stackoverflow but have not found anything similar.
radio button xpath:
//TBODY[#id='changeStartWeekGrid_rows_tbody']/TR[7]/TD[1]/DIV[1]/DIV[1]/DIV[1]
my code is:
List < WebElement > payDates = driver.findElements(By.xpath("//table[#id='changeStartWeekGrid_rows_table']//tr[position()>1]/td[position()=5]"));
//** Begin third inner for-loop****
for (WebElement pd: payDates) {
System.out.println("sample1-> " + pd.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date payDate = dateFormat.parse(pd.getText());
System.out.println("sample-> " + dateFormat.format(payDate));
if (payDate.after(new Date())) {
System.out.println("inside for loop");
String radiobutton = "//TBODY[#id='changeStartWeekGrid_rows_tbody']/TR[7]/TD[1]/DIV[1]/DIV[1]/DIV[1]";
WebElement calrow = driver.findElement(By.xpath(pd + radiobutton));
calrow.click();
pd in the loop should already contain the correct link i am thinking because it is looping through all the dates in payDates.
HTML code i loop through this is just a sample.
<td id="changeStartWeekGrid_row_1_cell_4" style="" align="left" class="table-grid-cell OUTPUT_TEXT" title=""><span>03/02/2018</span></td>
HTML radio button
<div class="revitRadioButtonIcon"></div>
I actually found what i needed this was my answer //table[#id='changeStartWeekGrid_rows_table']//span[text()='"+dateFormat.format(payDate)+"']/parent::td/preceding-sibling::td//div[#class='revitRadioButtonIcon']
If found out that you use preceding-sibling::td// to go back and td/following-sibling::td// to go forward here is another example
element = driver.findElement(By.xpath("//td//*[contains(text(),'Regular Hours')]//ancestor::td/following-sibling::td//*[contains(#class,'reactTextBox')]"));
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
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
From my application, I search for a name, the result opens in new window with the relevant employees in a grid, I need to know the number of occurences from that grid of that name.
Like if search for John results - John Fernandes, John Maxwell and John D souza. result sshould be 3 John's found.
This does not work:
WebElement text = driver.findElement(By.tagName("text"));
String bodytext=text.getText();
System.out.println(bodytext);
//Verify the Name searched
int count=0;
while (bodytext.contains(name)){
count++;
bodytext=bodytext.substring(bodytext.indexOf(name))+name.length();
}
By.tagName("text") will not work as "text" is not the HTML Tag name.
Use XPATH or better is CSS Selector.
You can use javascript also to count the number of names it returned.
If you can give the result page HTML I can help you in finding CSS Selectore or Javascript that can return you the result.
get the path to the grid ex.
WebElements gridElements = driver.findElements(By.xpath("//table[#id='grid1']/tr/td"));
system.out.println("No. of occ. is->"+gridElements.size());
Well it got working, the only problem was
bodytext=bodytext.substring(bodytext.indexOf(name)+name.length());
Let's take example from page http://www.w3schools.com/html/html_tables.asp
Here is javascript code for getting rows in table (It included heading row also)
document.getElementsByClassName('reference')[0].getElementsByTagName('tr').length
To run the javascript from web driver use
(String) ((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('reference')[0].getElementsByTagName('tr').length");
Hope this helps.