Find a unique locator Selenium WebDriver - java

I am trying to find a unique locator of the "update profile picture button" in my Facebook account to use it for automation test (selenium webdriver with java).
I use driver.findElement(By.className("_156p")).click();, but it doesn't work.
What should I use?!

Being unable to see the rest of the html source, I can't say for sure, but my guess is that the class is actually defined earlier in the source. The problem with using just a class name is that class names do not have to be unique on a page. The unique version of a class name is id, and if you are testing code that you can edit, try to use lots of ids.
If that's not the case, a decent way to deal with code that you can'y edit yourself is to use a css selector. Really good information on doing them here.
Another good debugging option is to use javascript or python to run a webdriver from the terminal. Because these languages are not compiled, you can run them in real time, which can allow you to tweak a class name a lot quicker. If you don't have experience with python, check this out. By using python/javascript you can create the webdriver, and then keep typing in driver.find_element_by_whatever("value_to_find") while on the same page. This will be much quicker than running the java program from scratch for every different "value_to_find".

As the page source you have provided is pretty limited unable to come up with a stable locator. How about getting the div using the text through xpath.
"//div[.='Update Profile Picture']"

Try This:
WebElement element = driver.findElement(By.className("_156n"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Worked for me.

Related

How about searching Google with Selenium?

I want to search something on Google using Selenium chromedriver and enter it. I can normally do this within the site, but I couldn't type it into google. What code can we use for this?
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");
Fakat olmadı.
The easiest way to do so is via the url when you perform driver.get(...).
Look at the google url for german shepards: https://www.google.com/search?q=german+shepards .
If you want to go the google.com and type in the search box you need to perform a better xpath query then by class name. As mentioned by #f1sh this is because google generates the class names (for scope based css). For me the following works for the search bar.
search_bar : WebElement = driver.get_element(By.XPATH, "//input[#title='Search']")
# then you can perform the send_keys
search_bar.send_keys(...)
Good Luck!
I think the problem is not the search text because I tried too many terms and it didn't work.
driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys("HBCV00000ODHHV");
 driver.findElement(By.xpath("//input[#class='desktopOldAutosuggestTheme-UyU36RyhCTcuRs_sXL9b']")).sendKeys(Keys.ENTER);
these are just an example on different sites. I tried id class type as xpath in every way, but it didn't write the text I wrote in the search bar.

Selenium Webdriver with Java for OBIEE Dashboard Application

I'm new to testing and trying to automate an OBIEE Dashboard application using Selenium Webdriver with Java. But, the problem is, the object identifiers I'm using (class, xpath, etc.) are dynamically generated, which leads to failure of my test case. Is there any way to overcome this? The scope of my test case is limited to testing the UI only.
My advice? Consider if you really, really, need to be using Java/Selenium approach.
A lot of OBIEE testing is better done at the logical layer using nqcmd or ODBC calls into the BI Server. If you really need to test the front end then visual testing is a generally more successful approach. The new Baseline Validation Tool covers both of these.
You can read more detail here:
http://www.rittmanmead.com/2014/01/automated-regression-testing-for-obiee/
http://www.rittmanmead.com/2014/05/visual-regression-testing-of-obiee-with-phantomcss/
http://www.slideshare.net/themoff/smarter-regression-testing-for-obiee-ukoug-15
https://www.youtube.com/watch?v=gMrspsqW0qM
You have to adjust the Generated XPaths as they may be not accurate.
for Example: You want to select this Div
<div class='blueBtn'>Click Me</div>
the XPath generated will be //div[#class='blueBtn']
This may select the first one but if this div is repeatable under another.
You may need to adjust the XPath to select what exactly you want.
So we may be adjust it to be //div[#class='blueBtn' and position()='2']
It's recommended to use the IDs of the elements as they are granted to be unique.
I hope this could help.

Selenium Webdriver is very inconsistent during Execution of test scripts

We have a Keyword driven framework which was developed using Selenium Webdriver.
While running the scripts some test cases are getting timed out in the first run. When I do the second run the same test cases which failed last time pass but this time some other test cases fail.
Can someone please advise if anything needs to be done on the Framework/Configuration part.
I am using IE9, Java 6, Selenium 2.40 on Windows 7 and IE driver from the official Selenium website.
Your tests could be brittle because of various reasons.
1. Synchronization- DO NOT USE Thread.sleep. You should consider waiting mechanism in your tests.
There are two types of waits in WebDriver. Implicit wait and Explicit
wait.
a. Implicit wait- For example below WebDriver will internally poll at max for 30 seconds before throwing NoSuchElementFoundException
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
b. Explicit wait- Here you are telling WebDriver to wait for a certain condition to satisfy. For example, below I am waiting the link Account to be available to click. Once its available WebDriver will return me the WebElement so that I can click on it. Take a look at some already implemented useful ExpectedConditions
WebDriverWait wait = new WebDriverWait(driver,30/*Timeout in seconds*/);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account")));
element.click();
2. Data Dependency- Make sure your tests are independent of each other and they don't share data. Tests can collide if they are sharing data which makes them brittle
3. Use CSS over Xpaths - Find my answer here as to why
4. A layer of abstraction- Make sure you have abstracted test logic from page logic. Use PageObjects and PageFactory technics for better maintenance of your suite
Finally read Simon Stewarts blog on Automated Web Testing: Traps for the Unwary for details.
I would suggest making sure that the keywords you are using are not dynamically generated.
I worked on a site once where all of the ID's looked like this: 'ext-gen123', then the next run through, the very same element would have the ID: 'ext-gen124', but the run through after that would have the ID back to 'ext-gen123'...
In this case, you'd have to use some other identifier to locate the element--maybe a CssClass or an XPath.
Sainath,
The Secret to run TestCases efficiently lies in the Way You wrote the TestCases.. Use of Fluent wait, Wait before the Element is Visible and Enabled and use of html ID's in Testcases makes the TCs Efficient. IE and Selenium Does not get along almost all time.. i mean,, There are so many Issues with IE and Selenium.
The Only way to Achieve Efficiency is by Proper Handling of Exceptions and Use of Wait statements

Get Xpath (or element) when page has randomly generated ID's HTML Unit

so I am using HTML Unit to click an item on a webpage. I usually use Xpath to select my items, but this page gives every element a randomly generated ID and class. I usually use Google Chrome to get the Xpath of elements, but it gives me something like this: //*[#id=":og"] where :og is the randomly generated ID. I know that sometimes chrome gives me Xpath without any ID's or Classes, like this: /html/body/table/tbody/tr[2]/td/table/tbody/tr[3]/td/form/table[2]/tbody/tr/td/input[2] Is it possable to get an Xpath that does not rely on IDs or Classes in a case like this?
Thanks.
In order to construct shorter xpaths or alternative ones based on tags only you can use plugins that will let you do just that. Particularly I favor the Selenium IDE in firefox, but in Chrome you can use things like Xpath Helper. There are others you can explore by searching the chrome web store.

Xpath Select Element Selenium

i am trying get a element through selenium with code:
WebElement a = driver.findElement(By.xpath("//div[#id=':r6']/span/text()"));
using this same expression on a firefox plugin, the element is find , but in selenium(java code) this way the element is not found, someone can me help
The command that you might need is: "AllowNativeXPath" - then just use the Xpath (either via Xpather or after 'inspecting element')to identify your element. Sometimes, though ... there's still an issue where Selenium does not 'see' elements described with an Xpath while running a script, but when users click the 'Find' button ... Selenium has no trouble at all. I usually take the focus up a level and down a level before any commands that Selenium has trouble finding the elements for ... and it works well thereafter. It's ugly and very NOT elegant ... but it works.
Selenium uses it's OWN Xpath interpreter ... and the one native to your browser might be better in some cases.
You can try this instead:
WebElement a = driver.findElement(By.xpath("//div[#id=':r6']/span")).getText();

Categories

Resources