Selenium Webdriver for IE XPath - c# - selenium-webdriver

Im using Selenium Webdriver for IE
I am having trouble sending a click to an image. The particular image is used throughout the page - I am attempting to find by xPath.
The html snippet:
<img src='Option.gif' border='0' style="cursor:hand;" onClick="javascript:DropDownOpen('ID_1_0_0_3_0_19');" />
The Webdriver Code:
driver.FindElement(By.XPath("//img[contains(#onClick, \"javascript:DropDownOpen('ID_1_0_0_3_0_19');\"]")).Click();

Your having a problem clicking the image because your xpath contains single quotes. To xpath to an element with single quotes you'll want to use xpath's concat() function. Your xpath becomes
//img[#onClick=concat('javascript:DropDownOpen(',"'",'ID_1_0_0_3_0_19',"'",');')]
You'll notice we concat javascript:DropDownOpen( in single quotes and anytime we need to target an element containing a single quote we wrap them in double quotes.
Happy Xpathing!

Related

How to get the text if it is outside the tag in selenium?

<i class="icon solr-icon icon-ic-city"></i>"Solapur"
So I want to get the text with the help of i tag, but the problem is
text "Pune" is out side the i tag. I can make use of li tag but I do not to do that. Can anyone tell me how to get text if the text is
outside the tag?
I dont believe that there is a specific option to do this is selenium and you would have to select the enclosing element and then retrieve the innerHTML to get the source including the text you are interesed and then parse the source by most likely just removing the tag.
element.get_attribute('innerHTML')

"How to fix 'at org.openqa.selenium.support.ui.Select.<init>' error in selenium"

I have created the object of Select in selenium to handle dropdown . Also have included the associated packages. Yet the dropdown is not getting selected. Kindly help!
Select select = new Select(driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[1]/header[1]/div[3]/div[1]/div[1]/div[6]/ul[1]/li[1]/a[1]")));
select.selectByValue("Blouses");
I am recieving the following error "at org.openqa.selenium.support.ui.Select.(Select.java:48)";
Alongwith a note when i hover over Select -
org.openqa.selenium.support.ui.Select
Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
As far as I can see your XPath expression ends with a which indicates <a> HTML tag which in its turn stands for a hyperlink
In order to be able to use Select class you need to pass to it's constructor a WebElement instance which will point to a <select> HTML tag.
If there is no <select> elements in your page source code - it means that the dropdown is being generated by means of CSS and JavaScript therefore you just need to click the link with the Blouses text which in its turn can be as simple as:
driver.findElementByLinkText("Blouses").click()
If you still want to use XPath - be aware that you can make it a lot shorter, readable and reliable: limit your search scope to hyperlinks only like //a and utilise text() XPath function to match only "interesting" links, the expression which will click the link with Blouses text would be something like:
driver.findElementByXPath("//a[text()='Blouses']").click();

Selenium webdriver convert CSS selector to XPath

This is for Selenium webdriver xPath. Please refer to screen capture. I am writing following XPath for Collection #1 button, but this is just temporary text which has been given to button. but this text will be change once BLL is implemented. So how do I define XPath? In screen capture UI and HTML is there.
For now I gave following xpath in my script.
//*[#class='infix' and text()='Collection #1']
This is xPath
/html/body/esx-root/div/esx-prospect-landing/esx-community-search-hero/div[2]/div[2]/div/button[1]
This is CSS
html body esx-root div.container esx-prospect-landing.ng-star-inserted esx-community-search-hero div.content-wrapper div.bottom-wrapper div.hero-actions button span.infix
This is CSS Selector
.hero-actions > button:nth-child(1) > span:nth-child(2)
How do I define short way XPath in my script.
You should go for some tutorial to write effective and customized xpath or css path
There are lots of tutorials website you can refer as per your interest.
https://www.guru99.com/xpath-selenium.html
https://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/
https://selenium-by-arun.blogspot.com/2012/12/25-locate-ui-elements-by-xpath.html
Always prefer relative xpath instead of absolute xpath and top of it, prefer CSS selector until you don't have final choice to using xpath (locate element based on text)
Now for your case you can refer below xpath to locate the element based on text
//div/button[contains(.,'Collection #1')]
OR
//button/span[class='infix'][contains(.,'Collection #1')]
And below CSS selector
.hero-actions>button span.infix
Additionally, you can take the other surrounding elements to make it unique.

Handling Dynamic Image click using Selenium Webderiver

I am working on image click functionality where images change dynamically but the XPath remains the same.
For example:
First image inspect element:
<a id="adlink" target="_top" href="http://www.jimhayes.com/lennielw/index-2.html">
<img id="adimage" src="http://egranary/adverts/Images/lightwire.png" border="0"></a>
First image XPath:
//*[#id="adimage"]
Second image inspect element:
<a id="adlink" target="_top" href="http://www.lifewater.ca/index.html">
<img id="adimage" src="http://egranary/adverts/Images/lifewater1.png" border="0"></a>
Second image XPath:
//*[#id="adimage"]
In this example source and target are changing constantly to redirect to particular page when clicked on image.
How to handle this situation using selenium webdriver?
I think your requirement is to basically check if the links are working fine - not broken.
In that case, I would suggest you to follow this approach - (It is in Java - you can do something similar in other languages as well)
I am trying to extract all href on the page. But if you want an specific xpath just change it here.
driver.get("https://www.yahoo.com");
Map<Integer, List<String>> map = driver.findElements(By.xpath("//*[#href]"))
.stream() // find all elements which has href attribute & process one by one
.map(ele -> ele.getAttribute("href")) // get the value of href
.map(String::trim) // trim the text
.distinct() // there could be duplicate links , so find unique
.collect(Collectors.groupingBy(LinkUtil::getResponseCode)); // group the links based on the response code
Now we could access the urls based on the response code we are interested in.
map.get(200) // will contain all the good urls
map.get(403) // will contain all the 'Forbidden' urls
map.get(404) // will contain all the 'Not Found' urls
map.get(0) // will contain all the unknown host urls
Check here for complete implementation.

Webdriver cssSelector of element with quotes inside of value

I cannot figure out how to select an element by cssSelector in my WebDriver test.
I will appreciate your help in solving this problem.
According to webdriver css selector syntax it has to be something like
WebDriver.findElement(By.cssSelector("a[ng-click='session='my_portcalls'']"))
but it doesn't work.
I hit a similar hurdle using Protractor/WebDriverJS.
What worked for me was the following:
element(by.css('[ng-click="changePanel(\'delete\')"]'));
In the html the element looks like the following:
.... ng-click="changePanel('delete')" ...
So single escaping works!
You need to escape the single quotes inside the attribute value for the selector to be valid:
WebDriver.findElement(By.cssSelector("a[ng-click='session=\\'my_portcalls\\'']"))

Resources