How to press/click the button using Selenium if the button does not have the Id? - selenium-rc

I have 2 buttons Cancel and Next button on the same page but it has only one id (see the below code). I wanted to press Next but every time it is identifying the cancel button only not Next button. How to resolve this issue?
<td align="center">
<input type="button" id="cancelButton" value="Cancel" title="cancel" class="Submit_Button" style="background-color: rgb(0, 0, 160);">
<input type="submit" value="Next" title="next" class="Submit_Button">
</td>

Use xpath selector (here's quick tutorial) instead of id:
#python:
from selenium.webdriver import Firefox
YOUR_PAGE_URL = 'http://mypage.com/'
NEXT_BUTTON_XPATH = '//input[#type="submit" and #title="next"]'
browser = Firefox()
browser.get(YOUR_PAGE_URL)
button = browser.find_element_by_xpath(NEXT_BUTTON_XPATH)
button.click()
Or, if you use "vanilla" Selenium, just use same xpath selector instead of button id:
NEXT_BUTTON_XPATH = '//input[#type="submit" and #title="next"]'
selenium.click(NEXT_BUTTON_XPATH)

In Selenium IDE you can do:
Command | clickAndWait
Target | //input[#value='Next' and #title='next']
It should work fine.

use the text and value attributes instead of the id
driver.findElementByXpath("//input[#value='cancel'][#title='cancel']").click();
similarly for Next.

For Next button you can use xpath or cssSelector as below:
xpath for Next button: //input[#value='Next']
cssPath for Next button: input[value=Next]

You don't need to use only identifier as elements locators. You can use a few ways to find an element. Read this article and choose the best for you.

You can use xpath for for identifying that element.

You can achieve this by using cssSelector
// Use of List web elements:
String cssSelectorOfLoginButton="input[type='button'][id='login']";
//****Add cssSelector of your 1st webelement
//List<WebElement> button
=driver.findElements(By.cssSelector(cssSelectorOfLoginButton));
button.get(0).click();
I hope this work for you

Related

Looking a way to automatically change one element on a website to another one. Hyperlink to a button exactly

So i have hyperlink at a webpage that is:
<b>456</b>
And i want it to be a button instead of hyperlink.
Like:
<input type="button" class="butt1" name="but" value="456" onclick="123.com'">
I tried to change it inside chrome dev tools and it works, well obviously since i jsut give it href adress manually. Sadly i have no expirience with tampermonkey or greasemonkey at all and very limited javascript knowledge. Wonder if it possible and would appretiate any help.
Here is a sample code based on your example...
First, you need to get the link
If it is only one link, find it based on a selector:
const a = document.querySelector('a.cs');
Create the input
const input = document.createElement('input');
input.type = 'button';
input.className = 'butt1';
input.name = 'but';
input.value = '456';
input.onclick = 'window.location.href=' + a.href;
Note: A better way is to use input.addEventListener('click', function) instead of onclick.
Replace the link with the button
a.parentNode.replaceChild(input, a);
If there are more than one links, then you need to get them and loop through them e.g.
const a = document.querySelectorAll('a.cs');

How to click on repeated button on a page?

I have a table layout like this:
<tbody>
<tr>
<td> 1 </td>
<td> <button> Click me </button>
</tr>
<tr>
<td> 2 </td>
<td> <button> Click me </button>
</tr>
....
</tbody>
I only know the value of the first td (1,2, etc). Is there anyway to click on the appropriate second td (the button) while only knowing the first td? For example, can I dynamically get 2 and know to click on the second "Click me" button instead of the first one?
yes there is way:
Basically, the buttons you want to click are siblings to the td elements you already know of.
Please try using locating an element via xpath sibling to locate those buttons:
//td[contains(text(),'1')]/following-sibling::* //this reads, first locate an element of td type whose text() attribute contains '1', then find its immediate sibling.
//td[contains(text(),'2')]/following-sibling::* //this reads, first locate an element of td type whose text() attribute contains '2', then find its immediate sibling.
You need an XPath:
twoClickMeButton = driver.find_element(:xpath,"//td[text()='2']/../td/button")

Finding the text of a label using Capybara

Finding the text of a label using Capybara
Background: I have a KBA page and a set of question and answers, the answers are in the form of 5 possible answers using radio buttons. So I need to cycle through each label for each radio button to match up with valid answers in a yaml file.
HTML:
<div class="questions"></div>
<div class="answers"></div>
<p>
<label>
<input id="answers_question_0_1" type="radio" checked="checked" value="1" name="answers[question_0]"></input>
RADIO BUTTON TEXT 1
</label>
</p>
<p></p> #another radio button and label text 2
<p></p> #another radio button and label text 3
<p></p> #another radio button and label text 4
<p></p> #another radio button and label text 5
My test code:
def answer_questions
.
.
.
i=0
def answers
page.all('.answers')
end
#This is accessing the answer value from the selected correct question from the kba.yml file
valid_answers = this variable contains the valid answer to the question
#********THIS IS THE PROBLEM BLOCK*****************************
#Set the radio buttons if they match one of the answers
#correct_answer = answers[i].all(:radio_button).find do |radio|
valid_answers.include?(radio.parent.text)
end
#********THIS IS THE PROBLEM BLOCK*****************************
i +=1
unless #correct_answer
p "Unable to answer question: #{question_text}" and next
end
#correct_answer.select
end
The problem lies in the "problem block" noted in the above code snippet. I can't figure out how to get the text that is tied to one label/p tag per radio button, instead ALL radio button's text are returned and checked against the variable "valid_answers" and that always fails.
I basically want radio.button.text to equal "RADIO BUTTON TEXT 1" for it's respective radio button. But instead radio.button.text returns:
RADIO BUTTON TEXT 1 RADION BUTTON TEXT 2 RADIO BUTTON TEXT 3 etc.
I'm guessing it shouldn't be radio.parent.text but something else, which I'm not sure.
It looks like you're making this more complicated than it needs to be - you should just be able to do
answers[i].choose("the text of the radio button you want to select")
You can catch an exception if the value isn't found and output your warning there.
Note: parent in a Capybara element is not the HTML parent of that element - it's the element that a finder was called on when locating the given element - so in your case it's the .answers element. If you do want access to the nodes actual HTML parent element you can call element.find(:xpath, '..')

XPATH- How to select radio button and text in a single xpath expression?

I have Radio button with value as "CREDIT_CARD" and the text of the radio button as "New". Now i need to select a radio button which has text "New".
<div>
<input type="radio" onchange="javascript:toggleAdvancedDisplay('pay_detail','CREDIT_CARD');" value="CREDIT_CARD" name="payment_type" style="margin:0; vertical-align: middle;"/>
<span class="value">New</span>
I tried the below xpath, but it doesn't locate the expected element.
/fieldset[1]/div/div/div[2]/input[#value='CREDIT_CARD']/fieldset[1]/div/div/div[2]/span[contains(text(), 'New')]
What is it i am doing wrong here?
You can try :
xpath = //input[#value='CREDIT_CARD' and following-sibling::span[contains(., 'New')]]
This will get input tag with CREDIT_CARD as value and whose sibling contains New as text.
The <span> element is not a child of the <input> element, but it is the next sibling. XPath should be:
/fieldset[1]/div/div/div[2]/input[#value='CREDIT_CARD' and following-sibling::span[1] = 'New']
Try this. Does this locate the element you're looking for?
xpath = //*[#value="CREDIT_CARD"]/span[contains(.,"New")]

To call a VBScript function on click of Enter key?

I have created a VBScript function OnRefreshList() which is called on the click of a button "Refresh List".
Sub OnRefreshList ()
Initdatatable(false)
if ReadFilters() = false then
msgbox "It is not possible to refresh the whole orders list. Please enter more filters"
exit sub
end if
End Sub
The "Refresh List" button is defined as
<td class="button cmd" valign="center" nowrap id="cmdRefresh" onclick="OnRefreshList()" title="Refresh the order list">Refresh List</td>
This function is working fine when I am clicking on the button.
Now I want to call this function when I press the Enter key from the key board.
For this I tried to change the following piece of code but it didnt worked for me.
<td class="button cmd" valign="center" nowrap id="cmdRefresh" onKeydown="vbscript: if (event.keyCode==13) then OnRefreshList()" onclick="OnRefreshList()" title="Refresh the order list">Refresh List</td>
Please help me if anyone has an answer to it.
Your onKeydown event does not have a correct VBScript statement in it. It should be:
onKeydown="vbscript: if event.keyCode=13 then OnRefreshList()"
Notice the single = sign and the lack of parenthesis (they are not mandatory). If you are more comfortable with javascript: You can mix VBScript and Javascript:
onKeydown="javascript: if (event.keyCode==13) OnRefreshList(); //this will work too!"

Resources