Selenium Java - How to click on an element without having it ID? - javascript

I'm trying to click in a radio button by it's Xpath or Id.
I inspected the element with Google Developer Tool and was able to get below details:
Element Name: <label for="1346_Voltagem_0"
class="dimension-Voltagem espec_0 skuespec_110v
skuespec_Voltagem_opcao_110V
skuespec_Voltagem_opcao_110v">110V</label>
Xpath:
/html/body/div[9]/div/div/div[3]/div[3]/div[1]/div/ul/li[2]/span/label[1]
I would like to know how to say to the code that it has to click on that radiobutton?
Click here for the page where I want to click the radioButton. Also, please refer the below screenshot for the exact element

Avoid using absolute xpaths as a small change in the UI will cause a lot of changes to be made in your code.
For your question, the following xpaths will work.
driver.findElement(By.xpath("//label[text()='110V']")).click();
driver.findElement(By.xpath("//label[contains(#class,'110v')]")).click();

Try this:
driver.findElement(By.xpath("//input[#class='skuselector-specification-label input-dimension-Voltagem sku-selector skuespec_110v change-image']")).click();

You should use xpath for this since you need to find the label and then find the previous input to it
You can find the label using
//div[contains(#class,'prod-sku-selector')]//label[.='110V']
and now to get the previous input you can use
//div[contains(#class,'prod-sku-selector')]//label[.='110V']/preceding-sibling::input[1]

I always believe that xpath should not be much dependent on your html divs and hierarchy as that is very easy to fail even with a smallest change. So, using xPath like "/html/body/div[9]/div/div/div[3]/div[3]/div[1]/div/ul/li[2]/span/label[1]" is not a good option.
In this case, I would suggest you to go with below xpath:
If first radio button needs to be clicked:
//div[contains(#class, 'prod-sku-selector')]//input[#type='radio'][1]
If radio button with value 110v needs to be clicked:
//div[contains(#class, 'prod-sku-selector')]//input[contains(#value, '110V')]
Then you can get element using :
radioBtn1 = findElement(By.xpath("//div[contains(#class, 'prod-sku-selector')]//input[#type='radio'][0]"));
Also, you can click radio in selenium using:
radioBtn1.click();

Related

add a javascript query dynamically change the button label in Oracle Apex

I have created a button named "Freeze".
I want to create a dynamic action that changes the name from "Freeze" to " "UnFreeze" on click.
I have set the static id for the button as "Freeze_StaticID" and then created a dynamic action for the click event.
Under True condition, I want to add a javascript query for the same.
Can anyone please tell me the query I need to add for the same?
I tried adding the code below but it didn't work.
$("#Freeze_StaticID").attr ('value', 'UNFREEZE')
It depends on the HTML implementation. When it's a <button> element, then it works like this: $('#Freeze_StaticID').text('UNFREEZE')
Btw: It's jQuery behind the scenes. You can toggle the browser's developer console (F12) and execute the appropriate getter and see what the result is for:
$('#Freeze_StaticID').text();
$('#Freeze_StaticID').attr ('value');
When undefined is returned, it's the wrong approach because it should return the current title of the button.
Details:
https://api.jquery.com/text/
https://api.jquery.com/attr/
Just use the JavaScript API for Apex
apex.item("Freeze_StaticID").setValue("UNFREEZE");

Calling a click function independently

I'm using the following spell checking javascript. The button that opens the spell checker is defined as button and if a icon, image or text is given the matching id, when this is clicked the spell checker opens.
I want to be able to call the spellchecker directly e.g. onclick="openChecker();" i even tried _openChecker() but cannot seem to call the correct function.
https://raw.github.com/LPology/Javascript-PHP-Spell-Checker/master/spellcheck.js
Can anyone help?
Thanks
The ID property should be unique in the first place so you should not have other elements with the same ID - one of the reasons is what happens to you, you get events triggered when elements with the same ID are clicked. You should fix this and if you do have elements that require the same identificator, you should use a class instead. As for your question you could do something like this:
$("selector").on("click",function(){
//your code here
});

Trying to use XPath for auto testing in (fillField)

Try to test some Fields and when i check "Inspect element" to identify id or name of Fields but there was only class that didn't work so i decided to use XPath for example:
$I->fillField('/html/body/div/form/div[2]/div[3]/div/div[1]/label/span[1]','GRV');
And gets Failures and Error:
1) Couldn't sign in in Authorization
Ups, I couldn't fill field "/html/body/div/form/div[2]/div[3]/div/div[1]/label/s
pan[1]","blabla",
Field by name, label, CSS or XPath '/html/body/div/form/div[2]/div[3]/div/div[1]
/label/span[1]' was not found on page.
I checked few times XPath, can't test this fields. It would be great if you tell what a problem and how to solved it.
You try to fill a label, not the input element:
//label/span[1]
Make sure to select the input field you want to fill instead. Look for an <input id="foo"/> if your label has an for-attribute with value "foo".
Well, avoid usage of this kind of XPaths. It's really toooo loooong. You just break your mind and ours too trying to solve it :) Just add #id selector to the element you are matching and be happy.
The test with such XPath will break on every markup change of your page. Why would you need a test that is such hard to maintain?

Click on a button with long url for name with javascript bookmarklet

I need either a javascript which can click on a button. The thing is, there are 100+ buttons on the page all with the same value. The name is unique but quite long.
The full name of the element is something like :
actions[http://apps.facebook.com/frontierville/giftaccept.php?next=giftaccept.php&senderId=1%3A1325206719&gh=3a8bfdace76051752a9127d1f9b43872&gift=nails&timestamp=1285598414&ref=tab&key=29b15e06ed9d7c00a8870c955ab938cf%24%24cfH1PUUZ%217bZYhg8M-o-XQc%218HHRMcvvyhuf4d%21.64qEvlQe&src=request&aff=gift&crt=nails&signature=6dd3fa03fe88f98b6dcab4faf4c7da94]
The value of every button is Accept and Play.
So. Is there a way to have it click on the button with a specific URL in the name?
I tried this:
javascript:(function(){var b=document.getElementsByName('actions[http://apps.facebook.com/frontierville/giftaccept.php?next=giftaccept.php&senderId=1%3A1325206719&gh=3a8bfdace76051752a9127d1f9b43872&gift=nails&timestamp=1285598414&ref=tab&key=29b15e06ed9d7c00a8870c955ab938cf%24%24cfH1PUUZ%217bZYhg8M-o-XQc%218HHRMcvvyhuf4d%21.64qEvlQe&src=request&aff=gift&crt=nails&signature=6dd3fa03fe88f98b6dcab4faf4c7da94]');for(var j=0;j<b.length;j++){if(b[j].value.match(/^Accept and Play/i)){b[j].click();break;}}})()
and it did not work. Any suggestions?
EDIT:
If it helps, here is the source of the info for one of the buttons:
<input value="Accept and Play" type="submit" name="actions[http://apps.facebook.com/onthefarm/giftaccept.php?senderId=1259413693&gift=mysterygift&timestamp=1285599906&ref=gift_accept_tab&key=78fcc7de3b36b8f9564262fab506893f%24%24ceK5RVRY61bZYhg8M-o-XQcyL%2CzHccEwEeuj4e-%21-dh0AD0A2AgyScd&signature=32db959ce43f8330cf8fd992fbd53a51&srcapp=FarmVille]">
your script works for me.
Are u sure, that you use the exact name in your script?
Are there other elements in the page, that share the same name?
If it does: you better also check the type of these elements. If they are not click()-able Elements(for example hidden-Inputs or text-inputs, this will cause an exception, so your loop will be stopped)

Get Element in div with JavaScript

I want to disable/enable a button with JavaScript. Because the JavaScript is called, after a Flash animation is rendered, the button exists at the time of the execution.
The button is in a hierarchy:
<html><body><form#form1><div#control><asp:Button#Export1>
I tried for hours to get a reference to that button, but nothing seems to work:
document.getElementById("Export1")
// and
document.getElementbyId("form1").getElementById("control").getElementById("Export1")
// and many more
How to get a reference to that button (in order to change btnref.disabled = true)?
Thanks a lot for your help!
Have you tried right-clicking in the document and selecting "view source" to see how that code actually renders? An asp:Button is a server control, that gets translated to an input field during render. During this, the ID of the field will not be exactly what you set it to in your aspx.
You can use Export1.ClientID at serverside to get the ID of the control.
If it's the only button in your div, this should work:
var btnref = document.getElementById("controls").getElementsByTagName("button")[0];
Usually the id of the button won't stay the same in the page source. Click on view source in the HTML and look for that tag to find the new id. You can then use that id in something like:
document.getElementbyId("Export1_some_unique_id")...

Categories