How can i click an image from the search results page - selenium

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.

Related

how to ignore span with List<WebElement>

I am trying to extract Main Text1 and Main Text2 from below DOM structure.
<div class="origination">
<div class="origin">
<h3>
Main Text1
<span class="info">Test1</span>
</h3>
<div class="test">
</div>
</div>
<div class="origin">
<h3>
Main Text2
<span class="info">Test2</span>
</h3>
<div class="test">
</div>
</div>
</div>
I have used below xpath with text() to identify header without span
#FindBy(xpath = ".//*[#id='origination']/div[*]/h3/text()")
List<WebElement> content;
I am trying to print header element without span using below code
for (WebElement element: content){
System.out.println("Header" + element.getText());
}
But I am getting error:
org.openqa.selenium.NoSuchElementException: Timed out after 15 seconds. List elements not found
However, when I used the same XPath on the firepath elements get highlighted in the DOM structure but not on to the page.
Here is the answer.
spanText = element.findElement(By.xpath("//span[#class='info']")).getText()
element.getText().Replace(spanText, string.Empty);

Unable to click an elemt " Search Intimation View-Details" thows not such element Exception

I am new to selenium,Guys please help me to click this element " Search Intimation View-Details".Cant able to use the ID has it is in number,& class name is not pointing exactly to that button.Guide me please,I'm strucked up.I tried
driver.findElementByXPath(" //div[span='Search Intimation View-Details'] ").click();
//
driver.findElementByClassName("v-tree-node v-tree-node-expanded v-tree-node-root v-tree-node-last ").click();
Below is the code
<div class="v-tree-node v-tree-node-expanded v-tree-node-last" id="gwt-uid-36" role="treeitem" aria-selected="false" aria-labelledby="gwt-uid-35" aria-level="2" aria-expanded="true">
<div class="v-tree-node-caption">
<div id="gwt-uid-35" for="gwt-uid-36">
<span>Intimations</span>
</div>
</div>
<div class="v-tree-node-children v-tree-node-children-last" role="group">
<div class="v-tree-node v-tree-node-leaf v-tree-node-leaf-last" id="gwt-uid-38" role="treeitem" aria-selected="true" aria-labelledby="gwt-uid-37" aria-level="3">
<div class="v-tree-node-caption v-tree-node-selected">
<div id="gwt-uid-37" for="gwt-uid-38">
<span>Search Intimation View-Details</span>
</div>
</div>
<div class="v-tree-node-children v-tree-node-children-last" role="group"></div>
</div>
</div>
</div>
I believe you are using Java selenium binding, so Use this code
driver.findElement(By.xpath("//span[normalize-space()='Search Intimation View-Details']")).click()
The below script uses the Java programming language and uses the CSS locators to find the required element if we are unable to find using the class, id, XPath, etc.
Use:
driver.findElement(By.cssSelector("div[role=treeitem][id^='gwt-uid-36']")).click();

how to select the elements dont have a particular class inside div

I have the html like below. And I want those webelments through CSSselector or xpath which don't have div< class="locked"
Can anyone help me. The output should come as two webelements which are
"I am free video1" & I am free video2.
Locked Video
<div class="item video " data-reactid="165">
<a href="/video/peppa/xyz" data-reactid="166">
<div class="LazyLoad is-visible" style="height:168px;" data-reactid="167">
<img class="visual-video" src="https://abc.jpg?w=300" alt="I am locked video">
</div>
<p class="text" data-reactid="168">Episodio completo</p>
<img class="video" src="/images/icon-video.svg" data-reactid="169">
<div class="locked" data-reactid="170">
<div class="opaque" data-reactid="171"></div>
<p data-reactid="172">Activa tu cuenta</p>
</div>
</a>
<p class="name" data-reactid="173">I am locked video</p>
</div>
Free Video 1
<div class="item video " data-reactid="185">
<a href="/video/ghi" data-reactid="186">
<div class="LazyLoad is-visible" style="height:168px;" data-reactid="187">
<img class="visual-video" src="https://ghi.jpg?w=300" alt="I am free video1">
</div>
<p class="text" data-reactid="188">Episodio completo</p>
<img class="video" src="/images/icon-video.svg" data-reactid="189">
</a>
<p class="name" data-reactid="190">I am free video1</p>
</div>
Free Video 2
<div class="item video " data-reactid="192">
<a href="/video/sddfo" data-reactid="193">
<div class="LazyLoad is-visible" style="height:168px;" data-reactid="194">
<img class="video" src="/images/icon-video.svg" data-reactid="195">
</div>
</a>
<p class="name" data-reactid="196">I am free video2</p>
</div>
This is relatively straightforward to do with XPath:
//div[contains(#class, 'video') and not(div[#class='locked'])]
Note that, strictly speaking, to avoid false positives, you should be handling the class attribute values properly:
//div[contains(concat(' ', #class, ' '), ' video ') and
not(div[contains(concat(' ', #class, ' '), ' locked ')])]
Are those Texts are constant? If yes, then you can take elements from text.
How do I find an element that contains specific text in Selenium Webdriver (Python)?
This is an interesting question and i came up with a idea.
My idea is to capture all the videos first and then remove the locked videos from all videos list. Code is below,
//Gather all the videos
List<WebElement> all = driver.findElements(By.xpath("//div[#class,'item video']/a"));
//Gather the videos which has locked
List<WebElement> locked = driver.findElements(By.xpath("//div[#class,'item video']//div[#class,'locked']"));
//Remove the locked videos from all videos
for(WebElement lock : locked)
{
all.remove(lock);
}
//Now "all" contains only the free videos.
for(WebElement free : all)
{
//Do the stuff
}
Note - Not tested. Kindly check and let me know if works
If your CSS implementation supports the :not() "pseudo-element" selector, then you could try
.item.video:not(div.locked) p.name
cf. https://www.w3.org/wiki/CSS/Selectors/pseudo-classes/:not
That may be overly specific as I don't understand the context of the mark-up.
For XPath, I think it would be something like this:
//div[contains(#class, 'video') and not(.//div[contains(#class, 'locked')])]//p[contains(#class,'name')

I am trying Selenium Webdriver to find input element by class name

How to find element input class="Test_type"? I successfully got by id=Test1 but failing to find element by input class="Test_type"
I have tried:
WebElement mainform = driver.findElement(By.className("mainform"));
List<WebElement> postadchildone2 = mainform.findElements(By.className("formrow"))
for(WebElement formrow: postadchildone2)
{
WebElement formfield = formrow.findElement(By.className("formfield"));
WebElement inputclass = formfield.findElement(By.className("Test_type"))
}
Also tried using CSS selector & XPath, however still it says didn't found, because of the same class name it is not finding input class.
Under for loop I tried doing
formrow.getText()
It displays all the text results but it is not taking class name? I am stuck please help
HTML:
<div class="mainform">
<div class="formrow">
<div class="formrow">
<div class="formrow">
<div class="formfield">
<div id="Test1" class="city_select">
<div class="blank1"/>
<div class="formrow " style="margin-left:10px;">
<div class="formrow">
<div class="formlabel">
<strong>You Are</strong>
</div>
<div class="formfield">
<label>
<input class="Test_type required" type="radio"/>
YouareTest
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
as you have the following piece of code:
<div class="formfield">
<label>
<input class="Test_type required" type="radio"/>
YouareTest
</label>
</div>
We need to outline input element somehow.
So I recommend to locate this element using CSS selector because it is the fastest way.
So 'input' tag has following class attribute= "Test_type required" , that means that to this element 2 CSS classes are applied:
Test_type
required
When locating with CSS selector element, an element B with class attribute y will be notated as B.y, an element with 2 class attributes y and z will be notated as B.y.z then.
So in terms of our task it will look like
input.Test_type.required
But also you can notice that you have another element - div with class attribute =formfield as parent element.
So to locate child element to parent with CSS in the structure like:
<parent class="zz">
<child 1>
<child2> abaracadabra</child>
</child 1>
</parent>
parent.zz child2 => in this way
So the second part of our selector be like :
div.formfield as it be parent selector in relation to input.
So solution combo =
WebElement inputt=driver.findElement(By.cssSelector('div.formfield input.Test_type.required'));
Please try this one, and tell whether it works for you or not.
Since you are trying to fetch with only partial class name, try using the below code
WebElement inputclass = formfield.findElement(By.CssSelector("Input[class*='Test_type'"))

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

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.