Find XPath for element with dynamic location on page (jump straight to element?) - javascript

I am trying to write Selenium tests for a webpage reloading alerts every so often as the database changes frequently. The alerts are in a <ul> as an <li>, and all have unique titles as an <h4> element below the <li> (with a lot of divs above this).
It'll look something like:
<h4 title="Title" class="message-title alarm-list-name ng-binding">Displayed Value matches Title Value</h4>
When I use Chrome's "Copy XPath" functionality, I get paths like //*[#id=": 0"]/a/div[2]/h5[1], but of course this changes every time the page reloads (and I still am getting "no element found" when I use this one). I want to know if there's a way to jump straight to the <h4> with its particular value, as this would be the easiest way to handle this problem, I think. I've tried //h4[#title="Title"], but I get no element found.
I've also tried driver.findElement(By.linkText("Text")); with similar results. I tend to think xpath will be better because it seems more versatile.
If you have other Selenium recommendations, I'd appreciate it if you could demonstrate using them with the webdriver plugin for Jmeter in javascript, since that's the tool I've been using.

A bit of more information would have helped us to address your query whether you want to extract the text Displayed Value matches Title Value or you want to want to invoke any method on it.
However as per the HTML you have shared as the WebElement is a Angular Element so you need to induce WebDriverWait with proper ExpectedConditions as follows (Java Solution):
To extract the text Displayed Value matches Title Value :
new WebDriverWait(driver, 20).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//h4[#class='message-title alarm-list-name ng-binding' and #title='Title']"), "Displayed Value matches Title Value"));
System.out.println(driver.findElement(By.xpath("//h4[#class='message-title alarm-list-name ng-binding' and #title='Title']")).getAttribute("innerHTML"));

Related

Selenium-webdriver crawler for Service-Now, Automating the search and opening of a task with python

I've been trying to get a selenium script to run on service-now.
What I need it to do is just to open SNOW and in my to-do list look for the first task called with a short_description of this exact string "Set Initial Account Passwords".
When I try going to inspect the element I have to do it twice, because the first time it doesn't expand where the element is.
If I copy the selector it looks something like this #row_sc_task_dc9cxxxxxxxxxxxxxxxx > td:nth-child(5)
If I copy the element it's this one: Set Initial Account Passwords
NOW, when I try to zero on that element it never finds it.
I even tried logging every element and sub element, every selector, I had it expand every table and whatever I could think of in a file.log and still, can't find either the sc_task, nor the string "Set Initial Account Passwords" nor the "ng-non-bindable"
I've tried everything I could find to read everything in the site, even to read what one can read as text and nothing.
Has anybody had to deal with this at some point?
I'm trying to automate an action and everything works fine, it's just this blessed thing that won't open tickets from the list and, having to specify each task number or what have you will just defeat the purpose of having this automated.
OH, and, I've already tried chatgpt and it's no good, it won't do what I need.
Here are 2 screenshots of what this looks like if that could help in any way.enter image description here
[enter image description here][1]
[1]: https://i.stack.imgur.com/XUeQw.jpg

Getting value from a div element in a online/running site

So Lets say I have a site like this one http://www.worldtimeserver.com/
and I want somehow to get the value of the time every second and write that value in a .txt file. I was going to use a OCR(optical character recognition) software for this job but ..... in the end this option wasn a good choice because I could not rely on exact position of the clock.
Then I started to think "is there a way to inject/put some code in the browser that would do this?". When I inspect the web-page (in Chrome) I saw that the Div containing the time has an id="theTime". So is there a way to do this? I have some basic experience in JS & DOM ... but this I have no idea how to do or from where to start. Also i would like to point out that I need to rely that the script will do this job for hours and hours and that the value of the clock is set by outside (a server).
If the value does not require the browser to refresh to change the value.. you can save it using localstorage and later copy paste it in a txt file
this is a possible duplicate.
Get value of input field inside an iframe
use an iframe (you can hide it if you don't want users to see it).

Wrong line after right click"inspect element" (selenium)

im new to selenium (and to html), and in the previous projects i used a bit of selenium when I wanted to select a dropdown value I just right click on the dropdown > "inspect element" and boom, i got to the line i needed which is always a type.
but now I got to a different one, this is what i did:
and this is where i got:
Dropdowns can be implemented in very different ways on a web page. Sometimes there is no select element involved at all.
In your case, the select element looks hidden since it has jcf-hidden class and the dropdown itself is implemented differently via a div element with select-area class, and probably javascript is involved to read/update options from a hidden select element.
If you know that it's the select tag you want, why not just use CSS selector to get that?
For example:
WebElement companyId = driver.findElements(By.cssSelector("select#company_id"));
or
WebElement companyId = driver.findElements(By.cssSelector("div.select-upload_company_id+select#company_id"));
depending on your need. The examples are in Java but can be translated easily to other languages supported by Selenium.

Statement fails to show or hide element

So I have this JS script for hiding category (div class=category):
http://jsfiddle.net/QN9cb/1/
Script should be working like this - Visitors who visit www.example.com can't see category, but visitors with link www.example.com/?access=beta can see that 'Beta' category.
I used jQuery .show() and .hide() to show and hide div category but category is not showing. Can't solve this for days already, my mind is blocked. Any suggestion?
//this line of code hides category
$("*").find("[data-categoryname=Beta]").hide();
//this line of code shoud show category, but it isn't showing it
$("*").find("[data-categoryname="+grabAccess()+"]").show();
//see link above for full script
PS. script is using cookies so the visitors don't have to enter every time /?access=beta
PS2. Only way to test script is to download it, because you need to enter /?access=beta in URL and you can't do that via JSFiddle
My best guess (without having set up a local test of your code) is that your rather odd selector is failing. The find() method searches inside an element, not through an array of previously selected elements. If anything, you'd use filter() with your current code.
Rather than this:
$("*").find("[data-categoryname=Beta]").hide();
I'd simply do
$("[data-categoryname=Beta]").hide();
or, assuming a div element:
$("div[data-categoryname=Beta]").hide();
The selector, as you have it, searches the descendants of every element on the page. That's a terribly inefficient way to approach this, and may be resulting in timeouts or other issues.

Testbox auto complete from string

I have a notepad file of about 10,000 words. I can export them as csv or tab separated values as required. Is there a way for my words to appear as suggestions in a textbox (input type text)?
This word work in the same way as google.
In HTML5 you have the datalist element which gives you a kind of autocomplete feature. Although I'm not really sure about what you want an answer to, for example it is probably not that efficient to put 10 000 words inside the datalist element.
You can use jquery along with some plugin for maximum cross-browsing capability.
Here is an example of what you are trying to achieve http://jqueryui.com/autocomplete/
Click on the vew source link on the page to see how it is done.
Edit:
Since you are using a lot of elements, why not creating an ajax request after the text change to load the elements you want and then stream them into a div right under the text box? This will make you more in control of what the user is seing and it will work on all browsers.

Categories