How can I find text location with Selenium? - selenium

I'm trying to find the location of some text on a web page using Selenium.
I can use the isTextPresent function to tell me if the text occurs, but then I want to know where it actually is.
The wider problem is that I want to click on this text.
The problem is that I don't seem to be able to click on this text, which I think is in some control embedded on the page. So, it doesn't seem to be detected as a link or button or option etc. However, I need to click on it to make a selection.
Any thoughts?

Your solution xpath=//*[text()='hello'] will click the first clickable element with the text "hello" in the source code. If you want to be more specific, you can add more cases to the xpath like this
xpath=//*[#id='exampleId']//*[text()='hello']
Now this will click element with text 'hello' that's found after some element with id 'exampleId'.

Xpath is great usage and you should answers above. However, I realised, that sometimes Selenium does not allow you to click something, because it thinks the text is hidden by CSS
So far I do not have any workaround for it and instead of clicking a button I am closing completly whole browser window.
But in my case its div hidden by CSS showing actual version number of such application. So I only take a screenshot of it:
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File destination = new File("path/to/outputfiles/versionNumber.png");
FileUtils.copyFile(scrFile, destination);

Got it !
I don't know the answer to how to find the location,
but, the more important bit is to click on that text.
I can just use an XPath locator in the click method, like :-
Click(xpath=//*[text()="hello"])
This will click on the element that has a text value of "hello".
In my case, this is unique, so that's specific enough.

${x_axis}= Get Horizontal Position xpath=//*[text()="Log files"]
Get Horizontal Position returns the position of 'Log files' wrt the left end(X-axis length).
the position is an integer value... so can be compared easily as well.

Related

How to verify text using selenium on popup notification before vanishing it

There is a requirement like verifying Text using selenium on Popup notification before vanishing.
Not find any suitable solution for this.Please help me in this use-case
You can use the below code to get text from popup after that you can assert with the expected text
String value =driver.switchTo().alert().getText();
it is really unpredictable to understand your scenario.We must need pop-up HTML and a screenshot, and what problem you are facing.Sometime the pop up might be bootstrap modal or alert, so without knowing the popup, it is not possible to give the solution.Besides you can try simply find the element and use getText() function.If it is simple modal(Bootstrap), it will give you the text.
If you are struggling to find the element you can pause the JavaScript by using F8.

How can I get the XPath expression from a web site's HTML content?

I want to use XPath expressions in Selenium code, but I don't know how to get it from a site's HTML code.
I'm using the Google Chrome web browser.
The easiest way is to inspect the element you want to get the XPath expression for. Then click on the highlighted code and Copy → Copy XPath which give you a full XPath patch you can copy to your code.
For Firefox, you should install Firebug or another extension like this.
As neliCZka suggests, you could also try to build a relative path if its possible by trying to find the proper XPath in the search bar in Chrome after inspecting the element.
I see that someone has downvoted the suggestions to use the Copy XPath option from different inspection tools. That's probably because these paths can often be difficult to maintain as the application changes, won't work in Selenium because they're not sufficiently unique, or can just be unreasonably complex for complex HTML.
Copy XPath... is useful as a starting point though. If you're not familiar with XPath, this tool can help you to get a better feel for how to apply it to your HTML content, but it's very important to learn how to use XPath yourself.
Right click on the element you want to have an XPath expression for, and then click Inspect Element. A new frame will open with the HTML source code.
Now right-click on the code and select "Copy XPath".
Note: you will have to remove " WITH ' after copying it into your code
Install Firebug for Firefox. ...
Open the website that you want to inspect. ...
Click the Firebug button. ...
Click the element inspector button. ...
Click the web page element that you want to inspect. ...
Right-click the highlighted code in the Firebug panel. ...
Select "Copy XPath" from the menu.
You can press F12 to get the check page, select your hoped HTML code, and hit the mouse 2. It has the copy option, and there is a copy XPath.

WebDriver - sendKeys(input) does not complete before sendKeys(Keys.RETURN)

My test is entering keys in an search text box, that's mostly ajax, and then pressing enter from the keyboard. There is no button to "start" the search so we're using the Enter key.
I'm using ChromeDriver as Firefox is not supported for our application with regards to this flow.
For my webdriver code (java), I'm having it do this:
searchIcon.click(); //opens the ajax overlay of the search text box
searchBox.clear(); //clears whatever text is already there
searchBox.sendKeys(input); //enters in the input text
searchBox.sendKeys(Keys.RETURN); //press return to start the search
The problem is, I noticed that the input text is not fully typed into the text box yet and the RETURN has already taken place. Basically, I'm doing a search of part of the input text that was entered.
Anyone encountering this? I tried to add an implicitwait before the Return action but since the elements are already loaded, that isn't going to help.
Try this, once you enter the input into the search box, check whether the text is completely present or not inside the text box again using if statement, if complete text is present, then do the sendkeys(keys.return) part.
Just give it a try. Because once you entered the text into text box, in the next step the element obviously will be there.
Thank you,
Shravan Kumar.T
I also encountered same issue while executing my test. And it get resolved by updating webdriver to the latest version.
webdriver-manager update --versions.chrome=2.33
webdriver-manager start --versions.chrome=2.33

openWYSIWYG - dynamic positioning

I am making a webpage that has different areas that can be clicked on to change the contents.
I want to set it up so that when you click on an area, an openWYSIWYG editor appears in the middle of the screen with the rest of the page greyed out.
I have achieved this except for the positioning of the editor. When you click on a section I have a div with 50% opacity cover the page and a textarea appear in the middle. But when I try to attach the editor to the textarea, it loses it's position and falls behind the div not in front of it like the textarea is.
Can anyone suggest a way of centring the openWYSIWYG editor?
You're looking for Modal positioning. Here's one solution:
http://jqueryui.com/demos/dialog/#modal-form
Basically, apply the Jquery Dialog to the div that holds your WYSIWYG. Don't forget a submit button.
Just a quick heads up--I went through quite a bit of headache dealing with WYSIWYG editors for client-facing sites a few months back. It seems that clicking the "paste from WORD" button was a very difficult task to figure out. Word-pasted code can wreak havoc on websites, overrunning text fields in the database. I gave it a try on their demo (which also doesn't support webkit based browsers) and the result wasn't pretty. The only two options I found to solve this were to sanitize code on the back end, or to use CKEditor. I chose the latter because it was so easy. Take a look at CK, it's a great piece of free code.

Use of 'ClickAt ' selenium command

I'm confused about the difference between the Click and ClickAt commands in selenium. Where can I use the ClickAt command?
Here are what Selenium IDE says about those two commands :
click(locator) Arguments:
locator : an element locator
Clicks on a link, button, checkbox or
radio button. If the click action
causes a new page to load (like a link
usually does), call waitForPageToLoad.
And :
clickAt(locator, coordString) Arguments:
locator : an element locator
coordString : specifies the x,y position (i.e. - 10,20) of the mouse
event relative to the element returned
by the locator.
Clicks on a link, button, checkbox or
radio button. If the click action
causes a new page to load (like a link
usually does), call waitForPageToLoad.
click is used when you just want to "click" on an element, like a button, a link, ...
And clickAt is used when you want to "click" on a position designated by mouse coordinates.
I suppose the second one can be useful for some "rich" applications -- I've actually never used it... On the other hand, I use click like all the time.
If you have a page with form elements, links, buttons, and stuff like that, you'll probably generally use click : it's way easier to find an element using it's id or classname than having to find it's position in pixels on the page ^^
I noticed some differences between click() and clickAt() when testing a ExtJS app.
For example, if I try to click a tab in a Ext.TabPanel, click() command does not work, although I provide it with an correct xpath, and clickAt() works fine.
Code looks like this:
click("//li[#id='tab-panel-id__second-tab-id']/a[2]/em/span/span")
doesn't work, but
clickAt("//li[#id='tab-panel-id__second-tab-id']/a[2]/em/span/span","0,0")
works.
Notice that coordinates are (0,0)
I can't figure out why this happens...
I'm testing a GWT application and it seems like I have to use clickAt if I want to click on a node in a tree widget.
Be careful when testing clickAt. Sometimes double clicking the command will cause it to show up red. You will change the line to try other alternatives but nothing will work. But then run your script and the clickAt line will be fine with whatever you type in.
There is a dojo widget at our application which only works with clickAt("//span[#id='mastheadIconBar']/span[1]/span/span","0,0").
Don't know why, but only click("//span[#id='mastheadIconBar']/span[1]/span/span") does not work.