Selenium Command - selenium

When we use selenium command at that time command not find and attribute not get? See below command.
<table>
<tr><td>open</td><td>http://www.wikipedia.org/</td><td></td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>assertAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>language</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>search1</td></tr>
<tr><td>assertAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
</table>
I am using Selenium IDE 2.5.0 in Mozilla Firefox and Ubuntu.

Xpath //div[2]#class central-featured is invalid. Try changing it to //div[#class='central-featured']/#class if you mean to select a class.
You could also use assertElementPresent function instead of selecting attribute, if the whole point is to check that element exists, i.e.:
<tr><td>assertElementPresent</td><td>xpath=//div[#class='central-featured']</td><td></td></tr>
Much simpler that way.

Use xPaths in this case.
Use google chrome's built in developer tool for this
Place your cursor on the element
Press Ctrl+Shift+C
Click the Element
That clicked Element's code is highlighted in the short window on the bottom
Right-Click on highlighted code
Select Copy > Copy XPath
Here it is you have copied the xPath for that specific element. This is shown in the image:
Click to see how to copy xPath

The Xpath you have used in Invalid.
You can use xpath as follows and through this you can fins xpath of any object - just need to study the concept:
Here as we can see we want to search Google Search just by writing its xpath in console
So to find the Google Search button we have to write xpath like this
//span[#id='gbqfsa']
Once we hit enter it would bring
[ gbqfsa">​Google Search​​ ],
It shows that xpath for Google Search Button is correctly written
Now suppose we want to search Google Search button if we are just familiar that id attributes start with gbqfs
then we have to use function starts-with like this
//span[starts-with(#id,'gbqfs')]
and once when we hit enter on console it would reflect two button one is Google Searchand Second one is I’m Feeling Lucky
[
gbqfsa">​Google Search​​
,
​I'm Feeling Lucky​​
]
So to find out the Google Search uniquely we need to complete id attribute to gbqfsa
“//span[starts-with(#id,'gbqfsa')]
and hit to enter and now it would reflect only
[
​Google Search​​
],
It proves that we have written right xpath for Google Search
In the same fashion we can use Contains function to find the Google Search button like this
here I have taken fsa from gbqfsa
//span[contains(#id,'fsa')]
hit enter and hopefully it will return
[
​Google Search​​
],
if there are multiple attributes then we can use:
//span[contains(#id,'fsa') and contains(#class, 'xyz')] hit enter and hopefully it will return
Information Source: Sumit Mittal Blog

You can use CssSelector as below
webDriver.findElements(By.cssSelector("div.central-featured")) // for more than 1 elements with same class
webDriver.findElement(By.cssSelector("div.central-featured")) // for 1 element

Related

Not able to locate webelement

I am not able locate a webelement, this web application opens in Internet explorer only and I have used all the possible ways to click but no luck.
Xpath locators that I have tried :
"//form[#id='Form1']//a[contains(text(),'Age Range')]"
and
"//form[#id='Form1']//a[#id='rptTables1_ctl07_hlTablename1']"
also I have tried click on the element using action class and javascript as well.
Attached DOM in the URL, please have a look here
In the node a, id value is not static so you can't locate that element using the id value but you can use partial id value for example, looks like rptTables1_ is unique in id value and the remaining part is changing so applying contains() on this may works.
Try the below xpath if there is only match :
//a[contains(#id, 'rptTables1_')]
Try the below xpath by providing the matching index if there are multiple xpath matches :
(//a[contains(#id, 'rptTables1_')])[Matching index number]
for example if the matching index is 3 then you can write like this (//a[contains(#id, 'rptTables1_')])[3].
Or you can use the Advanced Performance Parameters Panel Topics text to identify that element.
//a[contains(text(), 'Advanced Performance Parameters Panel Topics')]
Again if there are multiple matches then try to use indexing method as mentioned above.
Or you can try the below modified your xpaths :
//form[#id='Form1']//a[contains(#id, 'rptTables1_')]
or
(//form[#id='Form1']//a[contains(#id, 'rptTables1_')])[Matching index number]
or
//form[#id='Form1']//a[contains(text(), 'Advanced Performance Parameters Panel Topics')]
I hope it helps...
I haven't tried any automation in IE at all but in firefox, sometimes I encounter those issues and my work around other than the .click() function is send_keys(Keys.RETURN). Also, i'm using time.sleep(x) before clicking or sending keys to make sure the element has been loaded.

How to write a XPath for the text one4

I want to use XPath to locate a link behind a text.
I want to use XPath to locate a link behind a text. For example, locate "one4" by "what10". You can only use the text message "what10", but you can't use it in any other way, because the information on this page will change. I want to get is the "one4" link node.
<body>
<p>
so
<br>what1 one
<br>what2two
<br>what11one4
<br>what3three
<br>what4one1
<br>what5two2
<br>what6three3
<br>what7one3
<br>what8two3
<br>what9three3
<br>what10one4
<br>just return
<br></p>
</body>
For some special reasons, what I want to pass is that the text of what10 is positioned to one4.
Please help me.
You can use below line
WebElement loginLink = driver.findElement(By.linkText("one4"));
Selenium doesn't supports xpath-2.0 but uses xpath-1.0
The element which you are trying to refer i.e. which contains the text what10 is a Text Node and Selenium can't use it as a reference. So finding the node with text as one4 with reference to the text what10 won't be possible. As an alternative if the desired node is always the last but one node you can use the following solution:
xpath:
driver.findElement(By.xpath("//body/p//a[position()=last()-1]"));
Update
As per #MosheSlavin counter question here is the snapshot to demonstrate that the XPath works perfecto:

How to locate links from Google results

Scenario:
open "google.co.in".
click in the search input box.
type something.
click Enter.
get the text of all links.
The xpaths of some links are:
.//*[#id='rso']/div[2]/div/div[2]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[3]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[4]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[5]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[6]/div/div/h3/a
All the xpaths have the same pattern. the third div needs to be incremented by 1 to get the next xpath. I've read somewhere that in the scenarios like this generic xpath can be used. According to his suggestion, the xpath will be ".//*[#id='rso']/div[2]/div/div/div/div/h3/a". just removed the predicate of the third div. This is not working. Is this the way to locate elements?
You can try below XPath to fetch all result links:
//div[#class="g"]//h3/a
If you want to avoid links from "People also ask" block:
//div[#class="g"]//h3/a[not(ancestor::div[#class="_OKe"])]

selenium (phpunit) click on drop down link

I want be able click on link from drop down using selenium with phpunit. I don't have any idea how make it happens, can anyone show me example or relevant tutorial, post or anything that can help me figure out.
when I try click on the element without put mouse over the drop down I got this error:
Element is not currently visible and so may not be interact with command ....
Thanks.
EDIT:
when I said "drop down" I don't mean regular select. it more like popup
you can see the example here:
http://investing.com
look how they build the menu I want be able click on 'Technical' -> 'Fibonacci Calculator' for example.
Check this post out:
Selenium: How to select an option from a select menu?
You can find more info about this here
select(selectLocator, optionLocator)
Arguments:
selectLocator - an element locator identifying a drop-down menu
optionLocator - an option locator (a label by default)
Select an option from a drop-down using an option locator.
Option locators provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Locator.
label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
label=regexp:^[Oo]ther
value=valuePattern: matches options based on their values.
value=other
id=id: matches options based on their ids.
id=option1
index=index: matches an option based on its index (offset from zero).
index=2
If no option locator prefix is provided, the default behaviour is to match on label.
Credits go to Dave Hunt
What I use:
$search13 = $this->webDriver->findElement(WebDriverBy::id('id_of_dropdown_field'));
$search13->click(); // Clicking on the dropdownfield
$this->webDriver->getKeyboard()->pressKey('ARROW_DOWN'); // Will go down in your dropdown selection )
sleep(1);
$this->webDriver->getKeyboard()->pressKey('ENTER'); // Enter for submitting your selection
EDIT:
http://www.anitpatel.net/2012/02/25/selenium-webdriver-how-to-click-on-a-hidden-link-or-menu/
This one explains it in java but basically what he does is a mouse over/hover and wait.
Then he clicks on the element.
I'm not a java genius but it's an example how to work with it.
You can use the:
string mouseOver(string $locator)
This simulates a user hovering a mouse over the specified element.
http://docs.tadiavo.com/phpunit/www.phpunit.de/pocket_guide/3.1/en/selenium.html
Check if the element is visible using the xpath of the required option value.
$this->isElementPresent($xpath);
$this->click($xpath);
If it is true, then click() method selects the specified option.

Click a Link working in Selenium Ide but not working in Webdriver

I am testing a scenario for the site "https://www.freecrm.com/index.html"
login credentials [ john2013 / john2013 ]
Scenario :
1 open the site https://www.freecrm.com/index.html
2 login with valid credentials
3 click on the "New Contacts" link
4 Add new contacts
using Selenium ide i am able to login and click on the "New Contact" link , but when i am trying to do the same thing using Webdriver [ java] i am not able to click the "New Contact" link
the code i have written is given below
driver.findElement(By.name("username")).clear()
driver.findElement(By.name("username")).sendKeys("john2013");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("john2013");
driver.findElement(By.cssSelector("input[type=\"image\"]")).click();
The code up to the above is working fine but clicking the "New Contact" link
driver.findElement(By.xpath("//div[#class='noprint']/span[#class='headertext']/a[3]/")).click();
is not working though the same xpath is working in IDE.
i have tried with expected condition option , sleep but nothing is working.
can any one help me in this regard.
The problem is that your page uses a frameset. You should tell your webdriver which frame to use when it will search for your element. So try this:
driver.findElement(By.name("username")).clear()
driver.findElement(By.name("username")).sendKeys("john2013");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("john2013");
driver.findElement(By.cssSelector("input[type=\"image\"]")).click();
//switch the driver to use one of the frames on your page. Potentially wait for a bit till the page is loaded
driver.switchTo().frame("mainpanel");
driver.findElement(By.xpath("//*[text()='New Contact']")).click();
Please change the xpath of link to
xpath=//a[contains(text(),'<whatevertext>')]
Incase if the same text link is already present in some part o the page we can proceed with occurences. Example if it is repeating second time:
xpath=(//a[contains(text(),'<whatevertext>')])[2]
If Nothing is working the best way is through java script executor.
First get the webelement of the link.
Then use the following code"
webelement=driver.findElement(by.xpath("=//a[contains(text(),'')]
"))
(JavaScriptExecutor)driver.executescript("argument[0].click();",webelement)
Actually Speaking //div[#class='noprint']/span[#class='headertext']/a[3]/ is absolute XPath.
as it ends with a[3] the position will vary always so it is advisable to use relative XPath/CSS
Suggested CSS:css=.noprint > .headertext > a.classname
Another Suggested CSS: css=.noprint > .headertext > a[attribute='value']
So we can use either of the above format.