I am trying to create a program to select elements on the page, but it seems that the only thing that makes them unique are there titles. Is there any way of selecting certain things on a web page by their titles? I found this script but it will not select or click on the elements.
What am I doing wrong? Any help will do. Thanks
How can I get it to work on A and not div, sorry, that was the problem
My script that I am using --
$(document).ready(function () {
$("a[title=\"Learn More About Becoming A VIP\"]").click();
});
It says Div is not defined when I try to run script. Why So?
There is a way to select elements by their title attribute in javascript:
document.querySelectorAll('div[title="john"]');
======
You can also select elements that have a title beginning with john:
document.querySelectorAll('div[title^="john"]');
Or elements that have the (space-bounded) word john somewhere in their title:
document.querySelectorAll('div[title~="john"]');
Your selector should work if title value is john. You might wan't to select element that contains certain string as follow:
$('div[title*="john"]')
Related
Trying to learn some Javascript now that I have a few months of Python under my belt. Having trouble with HTML elements on a page which uses Javascript (not sure that is the right wording).
I have a simple Chrome Extension which I am trying to tell it to click a button when the page loads.
The DOM shows this code more or less:
<body class="skin-mark toolbar-open table-active">
<div class="tile-tables"></div>
</body>
My attempt at clicking the button has been multiple ways similar to this:
document.querySelector('#tile-tables').click();
Any help in understanding the process here and what I am doing wrong would be great! Thanks in advance! Feel free to correct me at any place and I will fix my language.
When you use getElementById you have to pass an element's id to it. There is only one element with an id in your HTML, the outer userActActions-51 - so, if you were to select by ID first, you would do
document.getElementById('userActActions-51')
and then you would access the second nested child:
const userActions = document.getElementById('userActActions-51');
const span = userActions.children[0].children[0];
span.click();
But it would be more convenient to use querySelector for this, which will allow you to use a selector string to select the span descendant of the element with the id userActActions-51 at once:
document.querySelector('#userActActions-51 span').click();
If the element might not exist, then make sure that it exists before trying to click on it:
const span = document.querySelector('#userActActions-51 span');
if (span) span.click();
I am using two different ways to access a web element with Selenium webdriver (JavaScript).
The first way uses a number indicating third div element in the parent div. The code is like this:
driver.findElement(By.xpath("//div[#id='sld-layer-container']/div/div/ul/li[2]/div/div[2]/div/div[3]/select/option[2]")).click();
This code doesn't work. It returns error: ElementNotVisibleError: element not visible: Element is not currently visible and may not be manipulated
The second way uses class to identify the specific div in parent div. Code is like this:
driver.findElement(By.xpath("//div[#id='sld-layer-container']/div/div/ul/li[2]/div/div[2]/div/div[#class = 'col-md-5']/select/option[2]")).click();
As you can see, the only difference is the identifier of last div element in xPath string. They should indicate the same thing. Magically the second one works but not the first one.
Here is a screenshot of css elements. The div highlighted is what I am trying to locate.
Can anyone help me with this?
Update 1:
As #Mahipal and #iamkenos required, I expended the div and now it is showing select and option. I thought the issue was only caused by not being able to locate the div but it seems not. Please help further.
you can try as below:
Select select = new Select (driver.findElement(By.xpath("//div[#id='sld-layer-container']/div/div/ul/li[2]/div/div[2]/div/div[3]/select")));
select.selectByVisibleText("PROJECT_VALUE");
you can try with below xpath as well...
//div[#id='featureClassList']//div[#class='col-md-5']
So I have this script which should automatically load up an Items tooltip on dom ready.
It works perfectly if I'd wish to only have the first found item to show a tooltip, but sadly that's not my goal.
The current code:
$(document).ready(function(){
$('#dbobject').each(function(){
$(this).load('data/fetch.php?id='+ $(this).attr('tdbid'), function(){
});
});
});
It works perfectly for the first item, and whatever I do I seem to break the script every time!
It currently looks for any href containing tdbid="ID HERE" tag, then load up the value of the ID from fetch.php.
How can I revise this code in a way so it will look up ALL links with tdbid in it's anchor and not only first one found?
Thank you in advance!
An identity has to be unique in the page to work properly. When you select an element by id, then you will only get the first one, because there is only supposed to be one.
Instead of selecting using the id, you can select all the anchor elements that has a tdbid attribute:
$('a[tdbid]').each(function(){
You're calling each on #dbobject. I assume you only have one of those. It sounds like you want to be doing $("a[tdbid]").each(...)
I have a bunch of textboxes that are created dynamically one per click.
They all have the same name "discount[]", same id "discount" and have the same class "tinput".
Is there anyway that I can use to change the text in all of these textboxes with javascript or jquery?
I have another bunch of the dynamically created text boxes.
The id and name are unique but the class remains the same.
If it gets the job done, I can change the class name.
Anyhelp is greatly appreciated.
Regards,
You definitely shouldn't use an id more than once per document. If you need it for a <label> you might want to consider putting the input in the <label>.
You are using the class attribute the right way, though. You can do:
$('.tinput').val('New value for all .tinputs!');
If you can't put the input inside the label you can use use this. I made this demo for someone a little while ago. It might be helpful. It'll increment an id like "id-1".
http://jsbin.com/APegOMo/3/edit?html,js,output
same id "discount"
Please, don't do that! "id" is to identify a dom element. That hurts in my soul.
If every id is unique you could access them by $("#myid").val("my new val"). Just an example.
Regards, - Tobbo
here is a simple example that takes all elements with a particular class name and changes the .html, not 100% sure I understand what you're after though. if you need to do someting specific with each divs text you can iterate through them by id (and maybe class as well) and change each divs individual .html.
if you can share a stripped down example of your code or elaborate I'd love to try to help. good luck.
$(".changeDivs").html("adfasdf");
jsfiddle example >>
Hi I am trying to use jQuery to select all elements which have the class of "icon" which do not contain a particular image (to turn them off for filtering content)
I could do something like the example below, but it would seem like there would be a much cleaner way to select all elements that don't contain that image src to hide them.
Thank you for your feedback!
$('.icon').hide();
$('.icon').find('img[src$="'+src+'"]').parent().parent().show();
$('.icon').find('img[src !="'+src+'"]').hide(); may work