What I am trying to do is to use HtmlUnit to get the href from the parent anchor of a span. Here is what I mean.
<span>Some Unique Text</span>
There is no id or name associated with either the or the tags, so going by this example, how would you find that link in a sea of others like it without ids or names?
Thanks!
use the XPATH //span[contains(.,'Transcript')]/parent::a/#href
(non tested, but XPATH is definitely the way to go)
Related
I'm trying to select an iframe in selenium java but the element doesn't have a Name or Id attribute. How should I get it? (I want to use the switchTo to switch to it)
The website I am trying to get the iframe from is jklm.fun and the xpath of the iframe is
"/html/body/div[2]/div[4]/div[1]/iframe"
(you need to be in a game for the iframe to exist)
Same way you select anything, based on their tree position: regular CSS selector. You query-select body div div div iframe and you're almost certainly going to get it (unless you have lots of three-deep divs with iframes in them, in which case what on earth is going on on that webpage =), but if that still gives you multiple options, narrow the query selector using tactical :nth-child(...) pseudo-classes.
I would not suggest to go with absolute xpath :
/html/body/div[2]/div[4]/div[1]/iframe
rather have a relative xpath.
Having said that, as far as switching is concerned, there are multiple ways:
Using driver.switchTo()
This basically provides 3 overloaded methods: -
with index :-
driver.switchTo().frame(index)
with name or ID:-
driver.switchTo().frame(nameOrId)
with web element
driver.switchTo().frame(frameElement)
so out of these 3, we can easily use 2 of them since id or name isn't available in your case.
Using the index:
driver.switchTo().frame("here pass the index if you know. ")
Using frame web element:
driver.switchTo().frame(driver.findElement(By.xpath("/html/body/div[2]/div[4]/div[1]/iframe")))
this should work for you.
The other way we have is with Explicit waits:
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/body/div[2]/div[4]/div[1]/iframe")));
This is more convenient, way to switch to iframe in angular or react based Application.
I am not getting any locators except full Xpath and also all the left navigation elements are deep inside tags?
I would suggest you to read any articles or watch any free tutorials e.g. copy selector in chrome
But what you can do right now is to try to modify your long Xpath by adding unique classNames or id or ...
Of course would be great to see your problem, for example a piece of code with DOM elements from where you trying to get your Xpath
I am currently working with Selenium and have now reached the interesting, yet incredibly difficult, world of CSS selectors.
I'm currently looking at selecting the different options of the Google tool bar. E.g when you search for something, on the results page you get options to search for the same term but under images, news, videos etc
I'm particularly interested in selecting the "Images" link.
I've been working on it for quite a while and the closest i have got is the below selector:
div a.q.qs[href]
This drills down into the right sub classes but there are 16 of them. Despite hours of aimless searching, i'm unable to complete the query with a contains method around the anchor text, which is unique in the targeted sub classes.
I'm aware there is a By LinkText option in Selenium, but i'm not sure if the anchor text is unique across the entire page. Also, i really want to understand CSS selectors in general so even if it was, i want to resolve this particular issue so i can apply it to future problems.
I'm looking for something like the below pseudo CSS selector:
div a.q.qs[href].Anchorcontains("Images")
Can anyone help?
All links have a unique parameter called tbm: its value is isch for images, so I'd go with
a[href*="tbm=isch"]
There are sometimes ways to get what you want with CSS selectors but if you want to find an element by contained text, you will have to use either link text/partial link text if it's a link or XPath for everything else.
The XPath for what you want is
//div[#id='hdtb-msb-vis']//a[.='Images']
You could use //a[.='Images'] but that returns two elements, one of which is not visible.
To break this down
// at any level
div find a DIV
[#id='hdtb-msb-vis'] that contains an ID of 'hdtb-msb-vis'
//a that has a child A at any level
[.='Images'] that contains text (.) equal to 'Images'
If you want to explore by link text, you can write something like
int count = driver.findElements(By.linkText("Images")).size();
and print count. My guess is that it will be 2, one of which is not visible. You can use Selenium to further filter this down to only the visible link, if you wanted.
You are going to have the same issue with BackSlash's CSS selector answer. You can tweak it slightly and solve this problem with the CSS selector locator
#hdtb-msb-vis a[href*='tbm=isch']
Here are some links to CSS references that will help you get started: W3C reference, SauceLabs CSS Selectors tips, and Taming Advanced CSS Selectors.
Is it possible to create an anchor tag that ignores a page? I.E place that tag on all page except the last page.
Note: I am using docusign with java. I cant create unique anchor text for the tags because I don't generate the pdf myself I just receive it as a byte array from a parameter.
This is my first question ever on stackoverflow I am sorry if I made any mistakes in asking the questions, If I did please point them out so I can learn.
No the platform does not have an option to apply anchor tags to specific pages, however you can configure things such that if a given anchor is not found anywhere to ignore that anchor.
The anchorIgnoreIfNotPresent property when set to true will make it so no error is returned if you try to match to an anchor that is not found anywhere in the document, so you might be able to use this based on your use-case.
anchorIgnoreIfNotPresent: True or false setting. If true, this tab is ignored if anchorString is not found in the document.
Reference
I'm running test automation software that will rely on "id" tags to recognize controls.
I'm developing in java on eclipse using the GWT plugin and have tried using both of the below methods to set the id tag for a button "add".
add.setId("addId");
DOM.setElementAttribute(add.getElement(), "id", "addId");
neither of these are modifying the id property correctly. Have you had this problem before or do you know a workaround?
Thank you for any help!
Jerry
If I remember correctly, several browsers (or probably just Internet Explorer) won't let you set a DOM element's ID after it has been appended to the DOM. This limitation would be there even if you are directly doing this hand coded javascript. The browser won't show any error on setting id attribute but won't update the attribute either.
So you need to set the ID before appending the element to the DOM.
EDIT
From discussion below it appears that you were assuming that setting ID on Button widget's DOM element will set the ID on a <input type="button"> DOM element. But this assumptions is not proving to be correct because Button widget wraps the <input type="button"> DOM element in other DOM elements (like table or div).
EDIT
You may want to try Button.wrap(element) method if you want to customize the input type="button"> element. First create (DOM.createButton()) or locate a DOM element, set it's id, and wrap it using Button.wrap(element)
Long time ago I had Selenium test suite for a GWT app and I used ensureDebugId method to set the ID.
Edit - It still seems to be part of the API