Xpath or cssSelector for displaying current time - selenium

enter image description hereI am trying to get current time with either cssSelector or xpath.
I am able to get curren time using: .is-today as cssSelector directly in date picker.
Can we call current time or current time plus certain minute using cssSelector or xpath?
As image show today's date has been capture with .is-today
html is
<div class="listContainer svelte-ux0sbr” xpath=“1”>
<div class="item svelte-bdnybl" xpath="1">09:05 AM</div></div>

Related

Selenium XPath find element where second text child element contains certain text (use contains on array item)

The page contains a multi-select dropdown (similar to the one below)
The html code looks like the below:
<div class="button-and-dropdown-div>
<button class="Multi-Select-Button">multi-select button</button>
<div class="dropdown-containing-options>
<label class="dropdown-item">
<input class="checkbox">
"
Name
"
</label>
<label class="dropdown-item">
<input class="checkbox">
"
Address
"
</label>
</div>
After testing in firefox developer tools, I was finally able to figure out the xPath needed in order to get the text for a certain label ...
The below XPath statement will return the the text "Phone"
$x("(//label[#class='dropdown-item'])[4]/text()[2]")
The label contains multiple text items (although it looks like there is just one text object when looking at the UI) in the label element. There are actually two text elements within each label element. The first is always empty, the second contains the actual text (as shown in the below image when observing the element through the Firefox developer tool's console window):
Question:
How do I modify the XPath shown above in order to use in Selenium's FindElement?
Driver.FindElement(By.XPath("?"));
I know how to use the contains tool, but apparently not with more complex XPath statements. I was pretty sure one of the below would work but they did not (develop tool complain of a syntax error):
$x("(//label[#class='dropdown-item' and text()[2][contains(., 'Name')]]")
$x("(//label[#class='dropdown-item' and contains(text()[2], 'Name')]")
I am using the 'contains' in order to avoid white-space conflicts.
Additional for learning purposes (good for XPath debugging):
just in case anyone comes across this who is new to XPath, I wanted to show what the data structure of these label objects looked like. You can explore the data structure of objects within your webpage by using the Firefox Console window within the developer tools (F12). As you can see, the label element contains three sub-items; text which is empty, then the inpput checkbox, then some more text which has the actual text in it (not ideal). In the picture below, you can see the part of the webpage that corresponds to the label data structure.
If you are looking to find the element that contains "Name" given the HTML above, you can use
//label[#class='dropdown-item'][contains(.,'Name')]
So finally got it to work. The Firefox developer environment was correct when it stated there was a syntax problem with the XPath strings.
The following XPath string finally returned the desired result:
$x("//label[#class='dropdown-item' and contains(text()[2], 'Name')]")

how can i handle dynamically changing ids on my webpage using selenium?

Here is my code
<mat-error class="mat-error" role="alert" id="mat-error-0"> </mat-error>
<mat-error class="mat-error" role="alert" id="mat-error-1"> </mat-error>
Here mat-error-0 will change to mat-error-1 on page refresh and mat-error-1 will change to mat-error-2 and so on..
There is no unique class or role attribute here that i can take here.
Please advise.
Use the following xpath
//mat-error[text()='Please enter a valid To date']
//mat-error[text()='Please enter a valid From date']
As long as innerHTML stays consistent after refreshing the page, you can use Xpath to find the element. e.g. the following Xpath can locate To date webElement.
//mat-error[contains(.,'valid To date')]
Use Following Xpath for dynamically changing id.
//mat-error[starts-with(#id,'mat-error')]
As per the HTML you have shared, as the id keeps changing so you have to construct dynamic xpath to identify both the elements as follows :
Element with text as Please enter a valid To date :
//mat-error[#class='mat-error' and starts-with(#id,'mat-error') and contains(.,'To')]
Element with text as Please enter a valid From date :
//mat-error[#class='mat-error' and starts-with(#id,'mat-error') and contains(.,'From')]

selenium webdriver find element in region

Working with automated testing, I have come across the following issue quite a lot of time: I want to find an element on the page, but the element has to be at a specific region of the page.
Take the following as an example:
I have a searchfield with type-ahead on the site. In my sidebar, I have the element I am seraching for (lets call it "Selenium"), but that is not the element I am interested in, I want to see if my type-ahead search is delivering the expected result when searching for "Selenium".
<body>
<header>
<searchfield>
<searchresults>
<a .... >Selenium</a>
<searchresults>
</searchfield>
</header>
<aside>
...
<a .... >Selenium</a>
...
</aside>
</body>
If I in selenium webdriver search for the linktext "Selenium" it will find both entries, the one in the sidebar aswell as the one in the searchfield.
Furthermore am I not able to wait for the searchresult with the WaitForVisible command on the linkText as Selenium will find the element in the sidebar and conclude that the element is preset.
So my question is:
"With selenium webdriver, how do I search for an element within a specific region?"
Poking around with this issue, I came across the use of Xpath. With this I could create "areas" where I want to search for an element. As an example, I went from
html/body/div/aside/div[2]/ul/li
to
//div[contains(#class,'coworkerwidget')]/ul/li
Now the code is MUCH more dynamic, and less prone to errors if our frontend guys edit something in the future.
Regarding the search, I could now set up something like the following code
//div[contains(#class, 'searchfield')]//div[contains(#title, 'searchfield') and contains(., '" + searchword + "')]"
First we specify that we want to look in the searchfield area:
//div[contains(#class, 'searchfield')]
I can then set some more criteria for the result I want to find:
//div[contains(#class, 'title') and contains(., '" + searchword + "')]
Some litterature on the subjects for further reading.
http://www.qaautomation.net/?p=388
http://www.w3schools.com/xsl/xpath_syntax.asp
Click a button with XPath containing partial id and title in Selenium IDE
Retrieve an xpath text contains using text()

Selenium Junit to find element

I am new to selenium.I am creating test script for duplicate registration into a website .When an error is displayed for duplicate registration i want to pass the testcase .
I am getting confused regarding the property of the webelement . I am not sure as to which field should i take or as how to write the findelement for the error image displayed.
Here is the HTML tag for the element image displayed:
<td>
<img src="images/bell.png">
</td>
This is for the text displayed along with error image :
<a onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['navigation'],'navigation:j_id322,navigation:j_id322','');}return false" style="color:#000000;" href="#">Found 1 patient(s) with same Patient Name, Gender and Age. Click here to view details</a>
Please help .Thanks
Based purely on the details provided, the following would work;
findElement(By.css("td>img[src='images/bell.png']")
and
findElement(By.css("a[onclick]")
However, I am sure that there is more context than this. Without the full html it is hard to give better solutions

How to highlight today's date in DateTextBox in dojo

Is there any way to highlight (show selected) current date in dojo DateTextBox when the text box is empty? I do not want to show the date in the text box (it should remain empty), but just to show today's date as selected.
I tried to use the 'dropDownDefaultValue' attribute provided by dojo for this, but it is not working (current value is not shown as selected or highlighted).
I am using dojo version 1.7.1.
Any suggestions in this regards that will be great.
If you look at the html that is used for the DateTextBox popup you'll see that the td for the current date looks like this:
<td class="dijitCalendarEnabledDate dijitCalendarCurrentDate dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" data-dojo-attach-point="dateCells" aria-selected="false" tabindex="0">
<span class="dijitCalendarDateLabel" data-dojo-attach-point="dateLabels">30</span>
</td>
If you want to style the current date so that it appears differently you should update add a css selector like
.dijitCalendarDateTemplate.dijitCalendarCurrentDate{
/*your styling */
background-color: green;
}