I want to select and click on the object present inside inner html (shown in the image). But the object is not getting identified. I am using Java.
Note --> my application not opening with any browser except Internet Explorer and I can't verify xpath from console/debugger so I have to verify it through code only.
Code I have tried so far but not working for me-->
option 1 -->
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a href[#text='Administrate']")).click();
option 2 -->
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a[#text='Administrate']")).click();
option 3 -->
driver.findElement(By.xpath("/html/frameset/frame[1]/html/body/ul/li/ul/li[1]")).click();
You are checking for exact text match, use contains instead
driver.findElement(By.xpath("//a[contains(text(), 'Administrate')]")).click();
Or
driver.findElement(By.xpath("//a[contains(., 'Administrate')]")).click();
Please note the difference between text() and #text
You need to change your XPath like
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a[text()='Administrate system']")).click();
Note:- Here you need to pass complete string for better learning please refer Firepath to create and evaluate your xpath
Also can use contains method to find an element on partial text basis
Related
I am working with JavaSE and Selenium WebDriver within Chrome. My task is to find a set of input fields and do stuff with them. The issue is that I have to do stuff in the presented order they are available on the web page.
So I would find them via XPATH, because that's what works in the given web page. Let's say that I have a set of inputs on the following path: .../form/div/div/div
However for reasons I cannot say, certain type of input fields (such as text and numbers) are in the following path: .../form/div/div
The problem is that one set of the inputs are one div 'deeper' than the others, so when I save them to a List<WebElement> with driver.findElements, i can't really save their order.
I thought of finding the inputs with id, but the id names have a space in it which Selenium apparently dislikes. I am not sure if relative XPATH could be of help in this case.
Your comments are appreciated.
I made the mistake of not reading enough about XPATH. What I was looking for was the 'and' operand within an xpath expression. If you are a beginner like me, please read about it on w3schools.
Basically the following code solved my issue, as a workaround:
driver.findElements(By.xpath("//input[#required=''] | //select[#required='']"));
I need to get the xpath of a header title that only provides the class and text itself within the 'inspect element' tab.
The goal is to have an if statement to check to see if the title of the page matches with my intended header so that I can run code that will command Selenium. The reason why I don't just use "find web elements by linkText" is because the actual content of the header is viewable in various pages. Therefor, my code would ALWAYS run since the text would always be present. I also cannot just use the class since the class name is the same for every page.
I need it to be specifically the header itself using BOTH the class name and text.
I have tried
if(driver.findElements(By.xpath("//*[#class='className' and contains (text(),'headerText')]")) !=null) but even if the header text doesn't match it will continue to run. I believe it would be because technically the class is present as well as the text itself although they are different elements.
Two possible points there:
driver.findElements(..) will never be null. It will return an empty collection if no matching elements found. So, use driver.findElements(..).isEmpty() to handle case when nothing was found
Try using contains(.,'headerText') instead of contains(text(),'headerText') if still no elements found. The first option will work when your header text element contains some child tags / formatting tags.
So I'm using Selenium for java and am trying to grab some text from a website after connecting to it. I know selenium has
driver.getPageSource();
But if there are variables it will return the variables and not the actual data, for example if the actual text I want is
13-11-28
What it actually is in the source is
/month
or something like that. How can I get the actual text using selenium or another library.
You could find the element and the perform 'getText' on it.
Example
driver.findElement(By.cssSelector(".month").getText())
I need a little help regarding searching an exact text using xpath in webDriver.
Suppose i have the html as follows..
<html><body>
<table>
<tr>
<td><button>abcd</button></td>
<td><button>abc</button></td>
</tr>
</table>
</body></html>
Now i want to click button "abc"
I used xpath as //button[contains(text(),'abc')] but it is always performing on button "abcd" as it also contain the text "abc". In this regards I need a predicate or some other procedure which can search exact text instead of containing text.
I also tried with //button[matches(text(),'abc')], //button[matches($string,'abc')], //button[Text='abc')], //button[.='abc')] and many more but none of these worked out to identify "abc" button.
I do not know if there is any problem regarding my xpath version as I'm not aware of the version. But I'm using java 1.6 JDK.
Though my exact scenario is not the example shown but similar logic needs to be applied.
Hence any help or suggestion would be highly appreciated.
I would use next xpath //button[text()='abc']. You have mentioned text() function but I'm not sure syntax was correct. Also you tried to use contains() -- it searches partial text and WebDriver gets first element found. I your case it is <button>abcd</button> button
To find the element 'abcd' you can simply use:
//button[contains(text(),'abcd')]
To find 'abc' use the normalize-space() function which will clean up your text for comparison purposes.
//button[normalize-space(text())='abc']
For exact search:
button[text()='abc']
For pattern matching search:
button[starts-with(.,'abc')]
//button[.="abc"]
The dot before the equality operator will do the text comparison. Another example is /PROJECT[.="MyProject"] from the xPath Java tutorial.
Try with ends-with instead of contains. If the buttons dont have unique attributes, you can add the parent hierarchy as well. Like //table/tr/td[1].
Using something like below worked perfectly fine for me.
//button[(contains(.,'abc')) and not(contains(.,'abcd'))]
For exact search:
.//button[./text()='abc']
Note: make you there is no space is available into
i am working in selenium i want access a text field and fill with any value but that field do not have any id attribute, so tell me how to locate that field.
You can use Cssselector or xpath.
you can find lots more on google . one useful link is -
http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.html
If you cannot add id to element, then you can use other options:
use other attributes (e.g.name or any other you have) - it is not reliable, because attributes can change in time. Example:
//div[#name='some_name']
use your HTML layout to locate element - it's even less reliable, because your HTML layout may be changed in time. Example:
//footer//div[position()=2]
In order to make it easy to write your custom XPath expression I would suggest to use FireFinder for FireBug FireFox plugin. It supports both CSS and XPath expressions and makes writing expressions really straightforward.
For typing into a textbox there is no need to have an ID. You can do it with XPath or CSS.