问题
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.
回答1:
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.
回答2:
- 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
回答3:
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
回答4:
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
来源:https://stackoverflow.com/questions/21797348/selenium-command