It's pretty straight forward. I have a String which contains an href value. I want to find the web element on the page that contains that specific href. I am using:
String href = "http://www.bing.com/images?FORM=Z9LH";
driver.findElement(By.cssSelector("a[href*='" + href + "']")).click()
If you inspect www.bing.com you can see that the first link "images" on the top left has the href value that I set here.
When I run, it says that there is no such element on the page. I feel like I am not using the correct "find by" option.
This should work for you:
driver.findElement(By.cssSelector("li#scpt0>a")).click();
As far as I can see my link appears as such:
href="/?scope=images&FORM=Z9LH1"
Therefore you may try to change your href variable's value. (if you're using chrome to see the source code, be aware that what you see in the F12 console and what you see with Ctrl+U is different. The Ctrl+U one is usually wha web parser see, before executing any JS or making subqueries)
Also, be aware that search engines (google, bing...) usually don't like being parsed, and they will do what they can to make your job impossible: using JS, iframe, changing patterns...
Related
I have to find element with "== $0" after end tag of "span". Below is the HTML Code of element.
<div _ngcontent-c6="" class="col-12">
<span _ngcontent-c6="">Registration with prefilled user's data</span>
</div>
Although while I have copied the html code it is removing "== $0" itself. so I am attaching image also.
I have tried to find out solution but it was not working. I have tried xpath that normally works like .//span[text()='Registration with prefilled user's data'], but no sucess. I just found that "we can access this element in chrome console with syntax ' $0' and it is working fine there
but I do't know how to find it with xpath and CSS or any recommended locator's strategies in Selenium.
Note: Please don't mention any work around say use className or css with class name like div.col-12 span as I knew already this. My problem is handling elements with == $0.
So the text, == $0, is not exactly what you think it means. This is just a feature of Chrome dev tools, and not an actual element on the page. It's a property used by dev tools that allows you to test scripts via console. This has been discussed here, and it does not affect Selenium's ability to locate elements on the page.
The issue might be the selector that you are using, or possibly a hidden iframe element higher in the DOM that is obscuring the element.
You can try this XPath:
//span[contains(text(), "Registration with prefilled user's data")]
I just swapped out the text()='text' query with contains(text(), 'text') which may account for any hidden whitespace within the span element.
This XPath is correct for the span element given there are no special cases on the page. So, if this does not work for you, it would be recommended to post the full HTML or link to the page you are automating, so that we can all help you better.
I have one doubt. Whenever I copy xpath from Firebug and try to use it in my selenium script, the functionality does not work. Or I get this error that unable to locate the element. But, certainly when I try to write expression and execute the same code snippet, it works fine. Why is it so, is there some problem with Firebug which is one of the popular tool.
suggestions welcomed.
There could be multiple reasons for the firebug generated XPath not to work in selenium.
Most common are two:
an element you are trying to find is not yet present in the DOM - this could happen when the page is not completely loaded, or is loaded asynchronously
there are iframe elements on a page. If you need to find and element inside an iframe, you need to switch to it first
Also, don't blindly trust the XPath generated by Firebug - most of the time it would not be the most reliable expression. If possible, operate id and class attributes and don't start your XPath expression from the root HTML element - make it relative (using //).
The issue here is not with the Xpath you are using,
First check where your element is located,
The reason here is your element is not visible to the driver,
The question here is why the element is not visible,
Work on this,
And Do provide the html code in your question so that it will be more helpful to resolve the issue quickly,
I would like to know how to identify via webdriver the following html "node":
thank you <em>very much indeed</em> - Angielsko-Polski Słownik <b>...</b>
(It's just any link of google when one launch a google search)
I have googled it, however I have found only cases where the id or the class were provided.
What about in this case?
This is my failing try:
webdriver.findElement(By.xpath("//a[#href='http://www.google.pl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCcQFjAA&url=http%3A%2F%2Fpl.bab.la%2Fslownik%2Fangielski-polski%2Fthank-you-very-much-indeed&ei=Sia8U6LPCevB7AagwoCICg&usg=AFQjCNF6y7swYrp3axD0hNrCWfjovhcVPw&bvm=bv.70138588,d.bGE']")).click();
Thanks in advance.
There are several possibilities:
By.tagName("a")
However, chances are there are more than one a tag, and so the above will pick the first one it encounters. To get more specific, you can use:
By.xpath("//a[0]")
0 in this case refers explicitly to the first a tag. However, to give a precise XPath answer, I would need to see more your page code, as well as your exact requirements. You can also use:
By.partialLinkText("thank you very much indeed")
This works best if you have unique enclosed text.
You may also want to read through the rest of the locators in the API.
If you are not able to identify the the link directly, you can try based on other element.
When you have any adjacent div or element having unique value, you can refer the link relative to that.
WebElemenet element = driver.findElement(By.cssSelector("div#id a"));
This will get the link element which is present in the div having an id value of "id".
I am getting the href by using
Jsoup.parse(hrefLink, "").select("a[href]").attr("href")
where hrefLink is founded hreflink.
What I want to do is, to get outgoing links from the current web page if they match with my condition. Unfortunately, because of anchorlinks, I cannot always get outgoing links, but instead I want to be able to get other hrefs that anchorlink is redirecting to. For instance:
Given page: http://en.wikipedia.org/wiki/Baked_potato
where citation [10] anchorlink has two outgoing links. I want to be able to get them. How can I do that by using Jsoup? If that's not possible with Jsoup, what else can I use?
HTML anchors (and links to fragments in general) only indicate a position in a document that a browser will scroll to when the anchor is navigated to (through a link or directly via a URL with a #fragment); they don't "redirect" to anything. The relationship between the links is not encoded in the document, so Jsoup (or any other library) cannot determine this, in general. Your program will need some semantic knowledge of the pages it's processing.
In your Wikipedia example, after finding the li#cite_note-10 element, you can select all child a elements, then use absUrl("href") to get the link target and filter out any links that refer to the same page. (Currently just checking that the href attribute doesn't start with # is enough, but in general a document can link to itself with a full URL too.) But this depends on the semantics of the document, not just its syntax -- a future Wikipedia redesign may move where the citation link points so that the outgoing links are no longer children of the citation link target, and your code will break.
selenium.click("gwt-uid-204"); // this is recorded from Selenium IDE
I am clicking the check box in my (gwt) java application. The gwt-uid is changing ever time, so if the id changed then my element is not found in my apps. The regular expression is not working for me and I am not sure what I am doing wrong. Thanks for your help
selenium.click("gwt-uid-[0-9]);
I am using selenium 1.0.3, Java
Many GWT elements comes with ensureDebugId (method on UIObject) to allow you to explicitly set Ids to elements for testing and debugging purpose. You also need to inherit the module
<inherits name="com.google.gwt.user.Debug"/>
to make it work. The advantage of this is, you can remove the trace from the production deployment by removing the inherited module in during prod mode compile. Hence there wont be code changes to remove unnecessary Ids.
You can do it in 3/4 ways
Can check this link :
3 ways of dealing with GWT dynamic element Ids
which talks about 3 different ways of assigning a static id to your GWT elements.
Also,
You can write a custom javascript method which will fetch all the ids dynamically. Then you can process those ids for selenium actions.
There are two possible solutions. The first is to tell Se that you are using a regex by saying regex:gwt-uid-[0-9]. As you have it there it is looking the and element whose name or id is that literal string.
The other solution is to turn on static id's for things which I discuss in http://element34.ca/blog/google-web-toolkit-and-id.
-adam
Assuming you have dynamic IDs, as you have presented, first realize that Selenium's click method takes a locator argument. A simple approach is to specify a locator that finds an ID starting with your constant "gwt-uid-" prefix. You can use any of these locators as the argument to your click method, depending on your preference of technologies:
== XPath ==
//input[starts-with(#id, 'gwt-uid-')]
== CSS ==
css=input[id^='gwt-uid-']
== DOM ==
dom=for each (e in document.getElementsByTagName('input')) if (e.id && (e.id.substr(0, 'gwt-uid-'.length) === 'gwt-uid-')) e
Footnote 1: I have not used GWT, so my examples above assume that it still puts a check box in an <input> element; adjust as needed.
Footnote 2: Selenium does offer regular expression support, as Adam intimated, but there are two issues with it in this case: (1) the prefix is "regexp:" rather than "regex:". (2) Selenium's click method does not support the regexp prefix at all! (My empirical evidence suggests that locators do not use regular expressions in Selenium, only text matching arguments do.)
You can also use Firebug add ons to remove the GWT UID.
Right click where the GWT UID is and select "inspect element with
firebug" -
Click on the code where the GWT UID is and when the firebug windows
appears select "deleted attributes id"
After removing the id, right click one more time and copy the "Xpath"
Add an extra (/) and paste the xpath on the target.
This may also help.