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

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!

Related

Realod AJAX Powered Page in Real Time without Starting from the Top Again?

Certain websites (think Twitter and LinkedIn) load new content via AJAX as you scroll down. Sometimes after scrolling to the end of a long AJAX loaded page (where no more items are being loaded) some of the content towards the top (or middle) is updated. Is there a way to reload the entire page without needing to start scrolling from scratch? In other words, without needing to scroll all the way from the top back to the bottom again?
I'm using Firefox with Selenium WebDriver (+ Java).
Could you provide more details about your goal? You're scrolling the page; the scroll cause going back to the page top. The question is, why is that a problem - that's the app behavior. If you provide more information on why it's not the behavior you're expecting, maybe there is another way of handling your case, other than with the use of WebDriver.
Cheers,
PM

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

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.

Why don't keyboard keys work in Selenium WebDriver?

I wanted to select an element through keyboard keys in Firefox. I am using this statement
driver.findElement(By.xpath("Element')]")).sendKeys(Keys.ARROW_DOWN);
But when I run it, I couldn't see any movement in the page. I am using JAVA to automate. This issue occurs even in BEHAT\MINK Tool.
My doubt:
is this feature not working because of the developers code?
or I need to modify my code to make it work?
For scrolling page up/down, you can send keys on body tag element of the page, like following:
driver.findElement(By.tagName("body")).sendKeys(Keys.UP); //to scroll up
driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN); //to scroll down

webdriver click on a link in a webpage

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.

Categories

Resources