webdriver click on a link in a webpage - java

I'm trying to click on a particular link in a webpage. Basically this is a Home button. So whereever I browse, I always find this button is same position.
As of now I use driver.findElement(By.xpath("//span/ul/li/a")).click(); command to click on this, but this is not working in all the webpages even though the xpath don't change. here is the firebug view of the link.
the id is not static, it keeps on changing. So what are the other ways I can detect this link ?

You can use By.linktext()
driver.findElement(By.linkText("Home")).click();

As dirkk said, you can always locate an element through a By.linkText() selector, although this could be a fickle solution: the "home" link text could eventually change (in which case your selector won't work anymore), there could be other "home" links on the page (in which case you wouldn't necessarily obtain the element you want) or the "home" link text might be different in a different language (in which case your selector will only work when testing the English site).
If you have control over the generated HTML code, you should try adding a static ID or even a class to this link and look it up through a By.id() or By.cssSelector() selector.
If you don't, try using a selector that relies more on semantics (IDs and classes) than structure (tag hierarchies). That way your selector will be more stable and easier to understand as well. But in the end, that's not always possible if you test an untestable external website.

Related

How to click on file-attachment icon using selenium (with java)?

I am creating automation that automatically sends messages through LinkedIn. The code is working fine, but as per our current requirement, I want to click on the attachment button so as to pass the attachment also with the text message. There are a few points that I have already tried:
Unable to fetch the element ID as its dynamic most of the time.
Don't want to use offsets (x,y) because once anyone changes the size of the window offsets changes.
Unable to use XPath.
Then I checked for another tool "UIVision" which makes a solution to capture that particular area (save it in a .png file) from the page where we want to click and during runtime it clicks there. So I tried searching image comparison API for java, tried with ASHOT and Sikulix too but none worked for me.
Anyone can help me with this?
When you say your unable to use xpath will you please elaborate?
The button has an id - it's attachment-trigger-ember1079.
If you're concerned about the latter part being dynamic, the attachment button also has a good title.
You can try using xpath string:
//button[contains(#title,'Attach a file')]
For me, on my linked in, this returns a unique hit on the expected attachment object:
If these don't work, there's also the option of using javascript to action the upload.
For more information on handling the upload dialog (after your identify the object) have a look here

How to use Selenium and Java to interact with dynamic web element?

I want to use Selenium to interact with an element on a website. This element contains further content depending on the user's behavior, but obviously has exactly one HTML-element the whole time.
The element looks like this when the mouse is not on top of it:
When the mouse is on top of it, it looks like this:
When the user clicks on the down arrow, other content is visualized:
As you can see it contains even more logic, I didn't add screenshots for it though.
The corresponding HTML code is that, nothing more:
I do not know how this content is created. Does anybody know how I can use Selenium and java to interact with that web element? Selenium is obviously restricted to HTML content - what can I do without trying any dirty hacks like positioning the mouse at a certain position on the element myself to trigger the different visualization?
Update
I want to do automated end-to-end testing. That means I need to programmatically use the buttons of the web element as a user would:
Delete the date by clicking the x button
Alter the date by clicking up and down buttons
Alter the date by using the calender component
Changing the text in the web element (I guess I might already be able to do that with my current knowledge of Selenium)
I talked to the developer of that application and he told me he actually just adds one HTML input element of type date to the application. The rest (i.e. the renderer/editor of that input field as I presented in the question above) is totaly browser-specific. It looks like that in Chrome and entirely different in Firefox.
The above described web element seems to be the regular HTML 5 DateTime selection in Chrome. With that knowledge you can find the web element yourself and a tutorial for Selenium here: https://www.guru99.com/handling-date-time-picker-using-selenium.html and another discussion similar to my question here: How to set HTML5 type="date" input fields (e.g. in Chrome) using Selenium/Protractor?
The guru-page obviously describes how to control the full web element and it seems to work. I will check this out and accept this as the answer for now. Thanks everyone for your time!

Selenium iFrame input text

I am using Selenium to build a test automation where the html is in an iFrame, I was able to find online the lines of code to activate the iFrame, click on a link, and press a button and they are working fine -see following lines:
driver.SwitchTo().Frame("06634000000BVL6");
driver.FindElement(By.LinkText("Loan Details R1")).Click();
driver.FindElement(By.XPath("//button[contains(.,'Edit')]")).Click();
I needed to input a text within a textBox in that iFrame, but I couldn't handle the ID or the Class, below is the HTML for the input:
Any thoughts ?
Thanks for your help
I can't comment yet, so this will be part answer, part comment.
Based on what you have posted, it looks like you may not be in the correct iframe and we don't have enough of the code from the webpage to tell if there is an additional iframe.
I'd also like to see the code you are using to write text to the field, you may have an issue there.
If you don't and the only issue you have is the selector then try the following. Go get the developer version of firefox. Navigate to your webpage in firefox. Inspect the element where you want to write text to. Once you are in the inspection screen, there is a path bar that can scroll left and right at the bottom of the screen. Check that to confirm that there is only the one iframe you mentioned and that you are in the correct iframe. If that is the issue, you should be good to go, you're code above works for switching between frames. If you are in the right iframe, then try a different method of finding the element for the text box. I have had the most success with hard to find elements by using the cssselector. To get the cssselector for the element right click on it, navigate to copy and then cssselector. From there your code should look like this (using c#):
driver.FindElement(By.CssSelector("INSERT CSS SELECTOR HERE")).SendKeys("Text");

Accessing an element with unknown id but known path

I am working on a testing program that operates with very little information. In this particular case, my program doesn't know the ID of elements in the page before it runs, because the Javascript on the page dynamically assigns those at run time. The only constants I have is the structure and the text I'm looking for. I'm including a screenshot of one example of the DOM being generated. In this case I know that I want to access the button with text apply that is displayed next to the label with the text "To Location:" Is there a way to use xpath manipulate their relationship and ensure that I'm accessing the right element. I can't just access the apply button because there are 6 apply buttons on the page with dynamically generated IDs. The label's next to them are different so I'm trying to use that and manipulate the path's from there. Help?
This is possible. If you provide the entire html code I could provide a better xpath. But for what you pasted, here's a simple one that might work:
//td[div//label[text()='To Location:']]/following-sibling::td[1]//button[text()='Apply']
There's a slightly longer winded way but thats generating a list of elements by class and clicking the one with the right text
`var elements = driver.FindElements(By.Class("text-pb"));
foreach(var element in elements)
{
if(element.Text.Equals("Searched Text"))
{
element.click();
}
}`
that might work thats if you want to click the button.
i use these sort of things on the pages works site generates so it should do what your after.

Selenium Webdriver: Checking all fonts on a page

For a Sprint-story, I need to assert that a certain font is not available anymore on different pages. Right now, I can verify that a certain element doesn't contain a font and is indeed replaced by another, but is there a way to check entire pages for the presence of a certain font?
I use Selenium Webdriver cucumber/Java within IntelliJ.
It depends on whether the font is specified through a FONT tag or CSS. FONT tags are obsolete now and discouraged from use but you could use the code below to find them and write the entire tag out to the console.
// find FONT tags
List<WebElement> foundFontTags = driver.findElements(By.cssSelector("font[face*='fontname']"));
for (WebElement foundFontTag : foundFontTags)
{
System.out.println(foundFontTag.getAttribute("outerHTML"));
}
To find it in the CSS is going to be a bit more tricky. Your best bet is to talk to dev or your design team and ask how the font might be specified (if you don't already know). The problem is that without that knowledge, you are going to be looking all over the place for the font. It could be specified inline, a STYLE block, in a separate .css file, and so on each of which is going to require its own search. I provided one example for searching for a font in an inline CSS style.
// find font-face in inline CSS
foundFontTags = driver.findElements(By.cssSelector("[style*='font-family:fontname']"));
for (WebElement foundFontTag : foundFontTags)
{
System.out.println(foundFontTag.getAttribute("outerHTML"));
}
If the font is not specified in an external .css file, you might be able to get away with a text search of the entire HTML source for "font-face:fontname".
One other caveat... just because you find a definition of a css class that contains the wrong font doesn't mean it's actually applied to a visible element. That's a whole other issue. You probably should get a good definition of what success looks like from the Sprint team.
You could use Selenium like so:
WebElement.getCssValue("text-align")
But this will be a bit tricky for all the CSS you'll have to validate. Here is a good article about that . Maybe a CSS unit testing like Quixote libs/frameworks will be a better fit. Other possible way (that I've used succesfuly) - visual regressions with huxley, it can support your entire layout presentation's validation.

Categories

Resources