Selenium selection By.Xpath in between a div - java

I am sure this question has been asked many times, and there are several examples of it. However, with there being so many different selection options, Id like the most clear cut way to handle selections of this sort. If the easiest way is not to use an xpath that is fine too.. Here is the puny HTML segment...
<div class="atcui-text atcui-align-right">SAVED DOCUMENTS</div>
I want to be able to identify it by the 'SAVED DOCUMENTS'.. what would be the easiest way to do this? Thank you!

You can do it with Xpath:
driver.FindElement(By.Xpath("//div[contains(#class, 'atcui-text') and contains(text(), 'SAVED DOCUMENTS')]"));
or if class is going to be dynamic then search only by text:
driver.FindElement(By.Xpath("//div[contains(text(), 'SAVED DOCUMENTS')]"));
This is c# hope you can translate it to java...

Related

Assert + Selenium 4 relative locators

I wonder if I can use the new Selenium 4 relative locators in my assertions.
For example something like that:
Assert.assertTrue("", myWonderfulLogo.above(myWonderfulNavBar));
There's a crooked way to do this but I wonder if there's a more straightforward way to do it: https://github.com/angiejones/selenium-relative-locators/blob/master/src/test/java/books/RelativeLocatorsTests.java
Thanks in advance!
If your goal is to write a test to see if your myWonderfulLogo is above myWonderfulNavBar, then relative locators can help you, although they will need to be converted to web elements first. All relative locators are doing is further constraining regular locations (xpaths, by id, by name, css selectors etc).
For example if I wanted to look for an element with a certain id, but I only wanted to look for elements that were above a certain other element (instead of searching the entire document) relative locators are good for that.
Say your myWonderfulLogo has the following html:
<img id="logo" ... />
And your myWonderfulNavBar has the following html:
<nav id="nav" ... />
Then to write a test to make sure your logo is above your nav bar, you could write something like this:
var myWonderfulLogo = driver.findElements(By.id("logo").above(By.id("nav")));
Assert.assertTrue(myWonderfulLogo.size() > 0);
If your logo isn't above the nav bar, findElements will have found no elements, so comparing if it's greater than 0 means that at least one thing was found. Hope that helps explain things.
Bonus food for thought: how will your test perform on different devices/screen sizes? With this new addition test developers will have even more to think about when testing responsive websites :)

Selenium WebDriver HTML Code

This is what the error displays when I use the xpath
What command on the selenium webdriver would I use to click a text which has a xpath of
html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
I tried using
driver.findElement(By.xpath("html/body/div[1]/div/div/div[2]/div[2]/div[2]/a")).click();
but still doesn't work... any suggestions?
Your xpath is incorrect. You are trying to hit an absolute path with html/body/div[1]/div/div/div[2]/div[2]/div[2]/a. You are missing a / so instead it should be /html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
try:
driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div[2]/a")).click();
I have seen similar issues when the element to be located was in a separate iframe.
Using FirePath or source html, check if there are any iframes and if yes, use
driver.switchTo().frame("framename")
and then use
driver.findElement(By.xpath("/html/body/div1/div/div/div[2]/div[2]/div[2]/a")).click();
What about just using By.linkText() or By.partialLinkText() instead of XPath? Using XPath in this way is "easy" but very fragile I would recommend against it.
driver.findElement(By.partialLinkText("EARN up to "));
You can, of course, edit the partial link text to be more or less specific depending on what you are looking for and how unique the text is on the page.
Since that didn't work, let's try a different approach. Let's find all the links in the table you have pictured above. The code below will grab the A tags in the table and then click the first one. My guess from the picture is that the text contained in the link will likely change so having a more flexible way to find the links is better than using the link text. See if the below works.
List<WebElement> links = driver.findElements(By.cssSelector("div.data-block-sub-popular-offers > div.row-heading > a"));
links[0].click();
Instead of using absolute xpath which is not reliable way to identify an element, you can try this: //input[#id='partnerId']/following-sibling::a. It finds an element with unique id and then locates nearest sibling having tag a
just use xpath below:
driver.findElement(By.xpath("//div[#class='row-40percent']/a[contains(text(),'Earn upto 2.5%')]").click();

selenium very similar xpaths?

I have two buttons on a page that have really similar xpaths -
the button im trying to click -
/html/body/div[#id='wrapper']/div[#id='content']/div[#id='contentarea']/div[#id='votecontent']/div[#id='votetext']/div[#id='voteboxes']/div[#id='votenow'][2]/form/input[2]
and the other button im trying to ignore -
/html/body/div[#id='wrapper']/div[#id='content']/div[#id='contentarea']/div[#id='votecontent']/div[#id='votetext']/div[#id='voteboxes']/div[#id='votenow'][1]/form/input[2]
the only difference between the two is the
[#id='votenow'][1]
and
[#id='votenow'][2]
but I can't figure out how to interact with the one that has the votenow[2], whichever way I go about it, it always seems to interact with the first one because that's the first one it finds
this is for java using the firefox driver, any suggestions would be great :)
Just find them both and get the desired one by index:
List<WebElement> buttons = driver.findElements(By.xpath("your xpath"));
WebElement secondButton = buttons.get(1);
First
Please talk to your developers! It is really bad practice to assign the same id to two different elements (in your case buttons) on the same page! It makes life for DEV and QA unnecessarily harder than it need be!
Second
The xpath-expressions you posted already contain the differentiation between these two buttons. So you just need to find the first one and click it.
via xpath:
You can use xpath - should be enough to search for the elements with id="votenow". As said before, you can be pretty precise in this case and already filter for the 2nd button:
WebElement button02 = driver.findElement(By.xpath("//div[#id='votenow'][2]/form/input[2]"));
button02.click();
via id:
As #alecxe already pointed out, you can also first go for a more general search of several elements and then filter the right one out. Personally I would use the id in this case:
List<WebElement> buttonWrappers = driver.findElements(By.id("votenow"));
// you want the button-input-element in the 2nd wrapper element, indexing starts at 0, so do this:
WebElement button02 = buttonWrappers.get(1).findElement(By.xpath("//input[2]"));
// since it seems there are several input elements below the desired div, you can use xpath again

JavaScript: Include/Append objects/buttons inside a <span>

I have a really long javascript code which basically has a form and generates buttons.
I want to place the buttons together inside a tag so i could fix up their appearance as a group.
How do I achieve this?
i know i basically have to use "document.createElement ("span")" but i'm kind of stuck there... please help
Do you use JQuery? it has .wrap() method, or when you generates form and buttons, directly wrap the buttons in a 'span', good luck!

Detect background color of a website

I am trying to detect color of different elements in a webpage(saved on machine). Currently I am trying to write a code in python. The initial approach which I followed is:
find color word in html file in different tags using regular expressions.
try to read the hex value.
But this approach is very stupid. I am new to website design, can you please help me with this.
There can be multiple stylesheets, and many cascading styles. You don't know which elements visually end up being the "background" elements. I think if you're looking for something robust that will work on most webpages, you need to leverage a browsers rendering engine and focus on identifying what a user would see.
Consider using a web browser to render the page, taking a screen shot, and then doing image processing to find the most frequent color near the sides of the page. You can use a scriptable browser like phantomjs.
If you're new to programming, this approach is going to be wayyyyy over your head.
In java you can use JSOUP. Its quite good
Document doc = Jsoup.connect("http://YourPage.html").get();
Elements colors = doc.select("[bgcolor]");
I don't know anything about Java or Python, but could you have it parse the html code and look for something like 'background-color: < color >'?

Categories

Resources