Selecting a WebElement With a Fluid ID? - java

I have an application which I am trying to test with WebDriver. On one page, the user selects a data source and some other options and then clicks next. When the user clicks next, they are presented with a page which has a list of documents that they must upload prior to submitting a request.
On the page with the documents that they must upload I am attempting to get the names of the documents and then compare them with a spreadsheet which has the list of required documents based on the data source the user chooses.
Here is my problem, I am trying to get the document names off the website. I first tried it with
String docOne = driver.findElement(By.xpath(/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div/div/h5)).getAttribute("textContent").toString();
System.out.println(docOne);
When I run this, it cannot find the element.
I then tried the following:
String docOne = driver.findElement(By.xpath("id('41027')/div[1]/h5")).getAttribute("textContent").toString();
System.out.println(docOne);
When I ran this, it could not find the element, I then manually tested this and found that every new request has a new ID, even though the first xpath I tested does not change.
My question then is, how do I get this to work? Since the element changes everytime I run the test?

I apparently had some brain flatulence this morning.
When automation clicks next to get to the documents page the page is scrolled all the way to the bottom and the top elements apparently cannot be found. I put a long delay 30 seconds and scrolled to the top of the page manually and the test passed with the first xpath I mentioned.
So I selected a web element which is always visible and told it to do a send keys with a page Up command, and it worked swimmingly.

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!

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.

Disabling an element in selenium webdriver

I am trying to count the number of rows in an HTML table using Selenium webdriver (Java.). The functionality is as below:
Text is iteratively entered into a search box from CSV file. Once, first text is entered and search results are displayed in HTML table, I have to count the rows in that HTML table.
The issue I am facing is that for every search text, the count of number of rows in HTML table displays same. I suspect that the rows are counted before the HTML table is refreshed for every search text and hence the count remains the same as the first text every time.
I guess the HTML table should be refreshed every time new text is entered in search box and then rows in table should be counted.
Can anyone suggest the possible solution to this issue?
Can I temporarily disable the HTML element of webpage till the new text from CSV file is entered?
As you cannot post your code
There is was a way but i am not how feasible it is for your problem
this can be achieved using java script
you can disable any element Using java script
Refer this for disabling
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_select_disabled2
1) using javascript executor you can execute it in selenium.
If First method is failing to do so
try
2)Execute your javascript first then run your selenium code means make your selenium method depends on the javascript method.
Let me know if this works out.

How can I save part of an HTML page by a user clicking on an element in browser on Android

I would want the part of the HTML file that gets highlighted when you inspect element. I just need it as a string to save. So a user chooses what element code to save by just clicking. Is this possible? Thanks
Edit: This is also not for a website I control, I need it for any website the user goes to.
The data URI will serve the purpose.
test
when click, a new page will come up with the content sdf.
You may use window.btoa(content) to generate the URI dynamically.
Yes, you will have to use ajax and json or jquery for saving a part of the html code to the data.

Categories

Resources