How to use webdriver to randomly click on a button with precondication - selenium

I'm trying to write a test script with flight booking tickets scenario like this: Randomly click a button which description contains keyword "tax not included",then results shows; Randomly click a button which not contains keyword, then price shows.
For instance: Go to ebay.com and search "iPhone",in the search result page,randomly click a url which label contains keyword "Buy It Now"...
Anybody got a clean solution to that? Thanks in advance.
I have no clue to handle this case...
here is my code:
public void flightSchedule(){
if (be.isTextPresent(locator.getValue("tax_text"), 1000)){ //if keywords displays
ArrayList<WebElement> lists = (ArrayList<WebElement>) be.getBrowserCore().findElements(contains(text(),'tax not included'));
Random random = new Random();
int ra = random.nextInt(lists.size());
WebElement element = (WebElement) lists.get(ra);
}
the page source code is:
<div id="itemBarXI151" class="avt_column avt_column_trans">
<div class="b_avt_lst">
<div class="avt_trans">
<div class="avt_column_1st">
<div class="avt_column_sp">
<p>
<span class="highlight">new york city</span>
(tax not included)
</p>
</div>
<div class="avt_column_2nd">
</div>
<div class="c6">
<div class="c7"> </div>
<div class="c8">
<div class="a_booking">
<a id="openwrapperbtnXI147" class="btn_book" title="booking tickets" onfocus="this.blur();" hidefocus="on" href="##" data-evtdataid="XI147">
<span>
<b>booking</b>
</span>
</a>
</div>
</div>
my xpath is:"//div[#class='avt_column avt_column_trans'] [contains(text(),'booking')]",but it doesn't work.

Related

Dynamic buttons added to the page, don't know how to find the element

I have a complected structure of the page and I have no idea how to find the element...
The page contains folders that are created by a user, I need to create a folder and then to click on it, but I have no idea how to find the element that I've created. The structure look like this:
<div class="row-text" style="width: calc(100% - 84px);">
<span class="row-item-name">
<span class="row-item-link">
<a class="grid-row-element-name">Eclipse111</a>
</span>
<span class="row-item-actions hover-child">
<a>Share</a><span> | </span><a watchdox-rename="name" watchdox-save-func="rename(element, name)" class="rename-link"><span translate="">Rename</span></a>
</span>
</span>
<br>
<span class="row-meta-data">
<span class="creation-date-formatted">Today at 10:30 | </span>
<span class="row-email">orgadmin#mailinator.com</span>
</span>
</div>
<div class="grid-row-buttons">
<div class="row-tools">
<div class="btn-group dropdown" uib-dropdown="">
<button type="button" class="btn btn-default uib-dropdown-toggle clear-button dropdown-toggle" uib-dropdown-toggle="" aria-haspopup="true" aria-expanded="false">
<span class="icon-wd-material-menu"></span>
</button>
<ul uib-dropdown-menu="" class="dropdown-menu-highZ contextual-menu dropdown-menu" role="menu">
</ul>
</div>
</div>
</div>
The class="grid-row-element-name" contains the name of the folder that was created (each folder has its own )....
I have no idea how to continue with the testing cause I am not able to click on the folder....
Thank you.
try the following, since you said "grid-row-element-name" has the folder name then trying using that class name in cssSelector.
List<WebElement> elements = driver.findElements(By.cssSelector("a.grid-row-element-name"));
String folderName = null; //name of the folder which you want to click.
for (WebElement ele : elements) { //Iterate over the loop
if (ele.getText().equalsIgnoreCase(folderName)) {
ele.click();//once the folder you want is found go for the click.
}
}
//OR
//To click on the last element try this
elements.get(elements.size()-1).click();
Since you are looking for an element with specific text that you have created (the folder name), I would approach this by looking for an A tag that contains the folder name.
BTW, you didn't tag your question with a language so the below is Java. You should be able to convert it easily to another language by just reusing the XPath, if needed.
String folderName = "Eclipse111";
// create folder
WebElement folder = driver.findElement(By.xpath("//a[text()='" + folderName + "']"));

Find WebElement base of text condition

I have an autocomplete list that opens once I type some text into a text field and each element in the list looks like this:
<div class="tt-dataset">
<span class="tt-suggestions" style="display: block;">
<div class="tt-suggestion">
<div style="white-space: normal;">
<p>
<span class="ok-circle"></span>
<strong>name</strong>
<a class="edit" href="bla bla">Edit</a>
</p>
<p>address</p>
<p>phone</p>
<p></p>
</div>
</div>
I get the list this way:
val list = driver.findElements(By.cssSelector("div.tt-dataset div.tt-suggestion"))
In some cases address and phone are empty. When this happens, I want to filter them out in my selector so that the list contains only elements where address and phone are not empty.
Any suggestions?

Autocomplete with Selenium

I'm having some (actually, a lot) of trouble automating selection from autocomplete options with Selenium. Currently, I am able to automate the inputting of the text, though, I am not able to select anything from the appearing drop-down suggestions list that pops up. I tried searching here for some answers to my problem, but nothing has worked. Below is the element that appears with the suggestions that I am trying to select:
<div class="cs-autocomplete-popup">
<div class="inner">
<div class="cs-autocomplete-Matches csc-autocomplete-Matches">
<ul>
<li class="cs-autocomplete-matchItem csc-autocomplete-matchItem">
<span class="csc-autocomplete-matchItem-content cs-autocomplete-matchItem-content" id="matchItem::matchItemContent">john doe</span>
</li>
</ul>
</div>
<div class="csc-autocomplete-addToPanel cs-autocomplete-addToPanel">
<hr>
<div class="content csc-autocomplete-addTermTo cs-autocomplete-addTermTo">Add "John Doe" to:</div>
<ul>
<li class="cs-autocomplete-authorityItem csc-autocomplete-authorityItem" id="authorityItem:">Local Persons</li>
</ul>
</div>
</div>
<div class="cs-autocomplete-popup-miniView csc-autocomplete-popup-miniView" style="top: 2px; left: 149px; display: none;"><div class="cs-miniView">
john doe
<div>
<span class="csc-autocomplete-popup-miniView-field1Label cs-autocomplete-popup-miniView-field1Label">b.</span>
<span class="csc-autocomplete-popup-miniView-field1 cs-autocomplete-popup-miniView-field1" id="field1"></span>
</div>
<div>
<span class="csc-autocomplete-popup-miniView-field2Label cs-autocomplete-popup-miniView-field2Label">d.</span>
<span class="csc-autocomplete-popup-miniView-field2 cs-autocomplete-popup-miniView-field2" id="field2"></span>
</div>
<div>
<span class="csc-autocomplete-popup-miniView-field3 cs-autocomplete-popup-miniView-field3" id="field3"></span>
</div>
<div>
</div>
</div></div>
</div>
From this, I am trying to select "john doe". Does anyone know an efficient/complete way of doing this? I would very much appreciate the help.
driver.findElement(By.xpath("get the exact field address of autocomplete textbox");
Thread.sleep(5000);
//for xpath: need to take the common xpath from the list of the elements.
List<WebElement> link=driver.findElements(By.xpath("//span[contains(#class, 'cs-autocomplete-matchItem-content') and .='john doe']");
for(int i=0; i<=link.size(); i++)
{
if(link.get(i).getText().equalsIgnoreCase("john doe");
{
link.get(i).click();
}
}

How can i click an image from the search results page

I am writing a test in Webdriver C# with Nunit. I have a search results page loaded with a list of products with images. I want to click the 1st image in the results.
I have tried using the Xpath (I used firepath to get this value)
[FindsBy(How = How.XPath, Using = "html/body/form/div[2]/div[4]/div/div[2]/p")]
private IWebElement ProductImage_xpath {get; set; }
When it runs it says:
Could not find element by XPath.
I would like to use CSS selector if possible as that is faster than finding by Xpath.
What syntax could I use to locate the image from the code snipped below?
Note: the hyperlinks are dynamic, I don't want to use the href.
public SearchResultsPage clickProduct_Image()
{
ProductImage_xpath.Click();
Console.Out.WriteLine("ProductImage = " + ProductImage_xpath.Text);
return new SearchResultsPage(Driver);
}
The Code snippet:
<div id="main" class="nav_redesign s-left-nav-rib-redesign" data-page-construction="aui" skeleton-key="results--searchTemplate listLayout so_gb_en --left-nav--shopping-engine">
<div id="topStatic">
<div id="top">
<div id="topAmabot"> </div>
<div id="searchTemplate" class="searchTemplate listLayout so_gb_en ">
<div id="topDynamicContent">
<div id="rightContainerATF">
<div id="rightResultsATF">
<div id="widthPreserver"></div>
<div id="centerPlus">
<div id="rhsAjax"></div>
<div id="resultsCol" class="">
<div id="centerMinus" class="">
<div id="atfResults" class="list results apsList">
<div id="result_0" class="fstRow prod celwidget" name="1780974728">
<div class="linePlaceholder"></div>
<div class="image imageContainer">
<a href="http://testerServer1co.uk/SpaceExplorer/dp/1780974728/ref=sr_1_1?ie=UTF8&qid=1409311161&sr=8-1&keywords=planets+1">
<div class="imageBox">
<img class="productImage cfMarker" alt="Product Details" src="http://ecx.images-test.com/images/I/51iYWWt1BqL._SL160_PIsitb-sticker-arrow-dp,TopRight,12,-18_SH30_OU02_AA160_.jpg" onload="viewCompleteImageLoaded(this, new Date().getTime(), 16, false);">
</div>
Xpath is very very bad choice to find element on the page =). As you said better use css selector. I don't know how your image presents on the page, but i'll write possible variants, choose the best one. So, using css selectors to find your image:
1. You can find any element using it's class
//will find "div class="image imageContainer""
driver.findElement(By.Css(".imageContainer"))
2. Combine searching by class and find first "a" child in that div
driver.findElement(By.css(".imageContainer > a"))
3.Find element using known attribute
driver.findElement(By.css("img[alt='Product Details']"))
4. Find element by ID
driver.findElement(By.Css("#atfResults"));
//or
driver.findElement(By.Id("atfResults"));
Well, it will be enough for you, i think. If you have any questions, you are welcome.

Locating element in a pagination bar

The button in the code "AS PDF" ,As excel" are wrapped in the button Export and the export button is embedded within a pagination toolbar
I am unable to locate this element in webdriver
tried
"//span[text()='As PDF']"
Still i see error unable to locate the element
<li class="leaf">
<button id="export" class="button capsule mutton up last over">
<span class="wrap">
Export
<span class="icon"/>
<span class="indicator"/>
</span>
</button>
</li>
ul id="menuList">
<li id="menuList_simpleAction.Report.exportReport" class="leaf">
<p class="wrap button">
<span class="icon"/>
<!--Item text goes here-->
As PDF
</p>
</li>
<li id="menuList_simpleAction.Report.exportReport" class="leaf">
<p class="wrap button">
<span class="icon"/>
<!--Item text goes here-->
As Excel
</p>
Please help
If you are using java code then you can use
driver.findElement(By.linkText("As PDF"));
or you can get all the icons using
List<WebElement> list = driver.findElements(By.xpath("//span[#class='icon']"));
for(WebElement ele: list){
if(ele.getText().equals("As PDF")){
ele.click();
}
}
Hope this helps you.