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')]"));
Related
I'm sort of new to Web Automation and I'm having some trouble interacting with clicking a value from a sub-dropdown.
I'm able to select the span, click the "Apply Special" link to make the drop-down appear, but no matter what I do, I can't hit the specific "li". I can get all the text from the span, but I don't know how to actually make it so it clicks the specific special.
#FindBy(css = ".sub-dropdown")
protected List<WebElement> specialsTest;
#FindBy(id = "ctl00_ctl00_content_content_ucOrderWorkflow_upnlApplyDiscount")
protected Element specialItems;
public NewOrderPage addSpecificSpecialToOrder(String special) {
Reporter.log(String.format("Add special %s to order.", special), true);
//This clicks the Apply Special link
specialItems.waitUntilVisible().click();
//This prints the content of the span, just to make sure I'm hitting the right dropdown
String text = specialsDropDown.waitElementsReady().then().getText();
System.out.println("Dropdown getText " + text);
//This is my attempt to find the <li> text and click it, but it's not working :(
for (WebElement li : specialsTest){
System.out.println(li.getText());
if (li.getText().contains(special)) {
li.click();
break;
}
}
return this;
}
Any help, would be highly appreciate it.
Thank you in advance, and let me know if I need to add more info.
As I saw, the element which you are trying to click is an a tag ,so you need to use correct locator to point this element. We can use:
Link/ Partiallink locator
Xpath locator: .//a[contains(.,'Percentage Discount')]
I have in my program a loop that prints a structure like this in each iteration:
<div class="grand-father">
<div class = "father">
<myTag>1</myTag>
<button>show more</button>
</div>
</div>
But after clicking the button the structure changes to:
<div class="grand-father">
<div class = "father">
<myTag>1</myTag>
<myTag>2</myTag>
<myTag>3</myTag>
</div>
</div>
So in my tests with page object model I'm trying to evaluate every myTag element in each "grand-father" element by doing:
List<WebElement> grandFathers = driver.findElements(By.xpath("//div[#class='grand-father']"));
for(WebElement gf : grandFathers){
//**Click the button of this father element to show more**
List<WebElement> myTagElements = gf.findElements(By.xpath(".//div[#class='father']/myTag"));
System.out.println(myTagElements.size());
}
My problem is that the last findElements for the myTag elements doesn't seem to be finding all the elements that weren't there before the "show more" button was pressed :( it's only counting one element even though all elements are already shown :/
Is there any way to tell selenium to "update" the variable according to the new html changes that weren't there when we first fetched the element? (Because I think that the problem might be that one)
Thanks if anyone can help!
It looks like you may have the answer already but for future readers, I would do something like this...
Grab all the DIVs with the class grand-father and loop through them looking for child BUTTONs. Click the BUTTON and then count the myTags.
List<WebElement> gfs = driver.findElements(By.cssSelector("div.grand-father"));
for (WebElement gf : gfs)
{
gf.findElement(By.tagName("button")).click();
System.out.println(gf.findElements(By.tagName("myTag")).size());
}
I solved the issue by refactoring all the code and clicking the "Show More" button when using findElements for the "father" elements, waay before using the findElements method for the myTag elements.
Keep this in mind when dealing with elements that will eventually show up in your DOM: First make all the operations that involve showing and processing data, then use Selenium to evaluate them :)
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());
}
I am using selenium-RC for my automation with java (Eclipse Kepler) . I am facing some issues with a selection of a option in a drop down list which is not in select tag it is in span.
I want to test a page where their is a drop-down list for selection of city name. the drop-down appears only when I give some value e.g for "Bangalore" I have to type "ban" so the drop-down appears and then I select the city Bangalore either with "mouse click" or either with "down-arrow and enter key" but when I run my selenium rc script it fails after typing "ban" the drop-down doesn't appears. i tried using both xpath and id in select command,click command. I am stuck over here please please someone help me to solve this issue.I think it is dynamic and based on JavaScript functions.One more ..The next two drop down are depended on the first drop-down(i.e the second and third drop-down are hidden and first drop-down is shown by_default.)
I am sending the link of page where i am trying to test script: https://pizzaonline.dominos.co.in
I tried using following commands:-
1:
w.click("//*[#id='homedeliveryform']/div[1]/span/a/span[1]");
w.click("//*[#id='ui-active-menuitem']");
2:
w.type("//*[#id='homedeliveryform']/div[1]/span/input","ban");
w.click("//*[#id='ui-active-menuitem']");
3:
w.select(" id=combobox", "value=BANGALORE");
4:
w.type(" id=combobox", "value=BANGALORE");
I've had a similar problem. This is how I solved it:
Find the arrow button in the combobox and click it, the dropdown list will appear.
The dropdown list doesn't really have anything to do with the combobox, it is a separate list wich is just displayed in a correct position so that it looks like it belongs to the combobox. The dropdown list is actually an <ul>-element, which doesn't seem to be very easily found. Anyway there are many <li>-elements with <a>-elements in them, and those hold the texts.
As i can see you are trying to select a webelement(Bangalore) from a dropdown which is not visible till the time you click on it. Here are the ways by which you are able to select a hidden web element.
1st way: it is not the problem to click any element using the same js. As you know how to get any option the last actions remaning is to perform a click. This should work for you:
WebElement hiddenWebElement =driver.findElement(By(..selector of the element....));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);
2nd way:
String cssSelector= ...//i gave them in your previous question
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
3rd way: using actions builder, advanced user actions API. You can read about it here And code will be smth like that:
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();
Hey thank you so much for the help. I solved this by
w.keyPressNative(String.valueOf(KeyEvent.VK_B));
w.keyPressNative(String.valueOf(KeyEvent.VK_A));
w.keyPressNative(String.valueOf(KeyEvent.VK_N));
w.keyPressNative("40"); // down arrow key
Thread.sleep(4000);
w.keyPressNative("10"); //Enter key
In the below scenario what I did is...(dropdwon is in the table)
clicked the cell
get all cell values from the table(probably the list will be configured as table or list .. here it is table)
traverse through all the cells.
click on the required value based on the cell value.
driver.findElement(By.id("ms__id3")).click();
WebElement table = driver.findElement(By.className("combo-list-table"));
allRows = table.findElements(By.tagName("tr"));
for (WebElement row : allRows)
{
cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells)
{
if(cell.getAttribute("text").equalsIgnoreCase("Business"))
{
cell.click();
}
}
}
Hope this will help...:)
I'm trying to fetch data from this webpage: http://www.atm-mi.it/en/Giromilano/Pages/default.aspx. Basically I'm using HtmlUnit in Java to interact with the "Route and timetable finder" in the middle of the left column, looping through each option in the select, clicking on "Find" and gathering the data I need from the resulting pages.
I've had no problem extracting data for urban routes, but can't seem to handle the radio buttons above: clicking on "Underground" in a browser, for example, should bring a new page with different options in the select below.
But I keep getting the same Select as before; to be more precise, I keep getting the same page (page2 has the same HTML code as page).
Clearly something must be going wrong in the .click() function, but what?
This is a simple version of my code:
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage("http://www.atm-mi.it/en/Giromilano/Pages/default.aspx");
HtmlRadioButtonInput radioButton2 = (HtmlRadioButtonInput) page.getElementById("ctl00_SPWebPartManager1_g_e31ad29e_62a8_401c_43ae_eb61300b4fc0_lines_type_rbl_0");
HtmlPage page2 = radioButton2.click();
HtmlSelect lineSelect = (HtmlSelect) page2.getElementById("ctl00_SPWebPartManager1_g_e31ad29e_62a8_401c_43ae_eb61300b4fc0_txt_dp_lines");
int size = lineSelect.getOptionSize();
System.out.println(size);
This is the radio button input HTML:
<input id="ctl00_SPWebPartManager1_g_e31ad29e_62a8_401c_43ae_eb61300b4fc0_lines_type_rbl_0" type="radio" name="ctl00$SPWebPartManager1$g_e31ad29e_62a8_401c_43ae_eb61300b4fc0$lines_type_rbl" value="0" onclick="javascript:setTimeout('__doPostBack(\'ctl00$SPWebPartManager1$g_e31ad29e_62a8_401c_43ae_eb61300b4fc0$lines_type_rbl$0\',\'\')', 0)" />
<label for="ctl00_SPWebPartManager1_g_e31ad29e_62a8_401c_43ae_eb61300b4fc0_lines_type_rbl_0">Underground</label>
The select:
<select name="ctl00$SPWebPartManager1$g_e31ad29e_62a8_401c_43ae_eb61300b4fc0$txt_dp_lines" id="ctl00_SPWebPartManager1_g_e31ad29e_62a8_401c_43ae_eb61300b4fc0_txt_dp_lines" class="dplinee">
EDIT:
Ok, so I've tried a different approach: since it looked like some kind of JavaScript engine problem, I figured I could try and disable JavaScript, carrying out the onclick action myself. This is the original JavaScript function:
var theForm = document.forms['aspnetForm'];
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
And this is what I did:
HtmlForm aspnetForm = (HtmlForm) page.getElementById("aspnetForm");
HtmlHiddenInput eventTarget = (HtmlHiddenInput) page.getElementById("__EVENTTARGET");
HtmlHiddenInput eventArgument = (HtmlHiddenInput) page.getElementById("__EVENTARGUMENT");
eventTarget.setValueAttribute("ctl00$SPWebPartManager1$g_e31ad29e_62a8_401c_43ae_eb61300b4fc0$lines_type_rbl$0");
eventArgument.setValueAttribute("");
HtmlElement submitButton = (HtmlElement) page.createElement("button");
submitButton.setAttribute("type", "submit");
aspnetForm.appendChild(submitButton);
HtmlPage page2 = submitButton.click();
All good, except I still keep getting the same page with the same old Select.
I know this is quite a long and boring question, but I thought I could update it anyway. I hope somebody will eventually have the patience to try this out (and at least confirm I'm not doing some obvious mistake).
I finally found a way to make this work. The second approach was almost right. I was correctly submitting the form, but with a difference from normal browsing: I didn't actually check the radio button. Apparently, the destination page used that information too. By adding this
HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) page.getElementById("ctl00_SPWebPartManager1_g_e31ad29e_62a8_401c_43ae_eb61300b4fc0_lines_type_rbl_0");
radioButton.setChecked(true);
to my previous attempt the submit action worked perfectly. I still don't know why the .click() method didn't work as expected, though, but this is good enough for me.