className, tagName etc in selenium java - selenium

I tried to click the button using selenium.
The code of the button is as follows:
<button class="btn btnSearch" type="button" data-gtm="wyszukiwarka" szukaj">Szukaj</button>
I do not see any id?
It's possible to click the button like this?

While there are many ways to click a button without name and ID, and there are many duplicates of this question, my advise is always to use a relative XPath expression when there is no name or ID.
You can click the button with:
findElement(by.xpath("//button[#class='btn btnSearch']"))

As per the HTML provided to click on the button with text as Szukaj you can use the following line of code :
Java :
driver.findElement(By.xpath("//button[#class='btn btnSearch' and contains(.,'Szukaj')]")).click();
Python :
driver.find_element_by_xpath("//button[#class='btn btnSearch' and contains(.,'Szukaj')]").click()
Dot Net :
driver.FindElement(By.XPath("//button[#class='btn btnSearch' and contains(.,'Szukaj')]")).Click();

Related

How to find element of the below HTML?

Am curious if anyone might know which Selenium locating element method would be used to identify the below html.
I am trying to locate and 'click'
button type="submit" tabindex="3" data-ng-click="login()" class="btn btn-default" data-ng-disabled="loginForm.$invalid" data-ng-class="{ 'gray': loginForm.$invalid }">Login</button
It depends, if you know text is not gonna change, direct use text
//button[text()='Login']
or based on attributes
//button[#data-ng-click='login()']
You can combine these two like below :
//button[#data-ng-click='login()' and text()='Login']
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
You can following xpath,
//button[#type='submit']
//button[#data-ng-click='login()']
//button[text()='login']
driver.findElement(By.xpath("//button[text()='X']")).click();

selenium code for accessing button type submit in c#

I am trying to extract the text in an input box,
<input value="Add a Phone" onclick="CSS.addClass($("u_0_4"), "async_saving"); new AsyncRequest().setURI("\/ajax\/phone\/confirmation").setData({source: "www_mobile_settings"}).setStatusElement($("u_0_4")).send();" type="submit" id="u_0_5">
I have tried for ;
driver.FindElement(By.Id("u_0_5")).Click()
but raising an error of : No such element Unable to locate Element
The element Add a Phone is present inside an iframe.You need to switch the iframe first in order to access the element.
driver.SwitchTo().Frame(driver.FindElement(By.CssSelector("iframe[src^='https://www.facebook.com/settings?']")));
driver.FindElement(By.CssSelector("input[value='Add a Phone']")).Click();
In order to get that button, you can use CSS selector here is one
driver.FindElement(By.CssSelector("input[type='submit'][value='Add a Phone']")).Click()
or by XPATH
driver.FindElement(By.XPath("//input[contains(#value,'Add a Phone')]")).Click()

Continue Button - Dynamic value

I have 3 registration pages and each page has continue button. I want to use one method for all 3 pages but they have same xpath with different id.
eg
First page xpath: //div[#id='personalInfo']//button[#class='btn btn-primary btn-block'][contains(text(),'Continue')]
Second page xpth: //div[#id='AccountInfo']//button[#class='btn btn-primary btn-block'][contains(text(),'Continue')]
Please let me know how can i use dynamic as I used or for #id but work for first page but in second page it says element not found.
Thanks
I used or for #id but work for first page but in second page it says element not found.
First page xpath: //div[#id='personalInfo']//button[#class='btn btn-primary btn-block'][contains(text(),'Continue')]
Second page xpath: //div[#id='AccountInfo']//button[#class='btn btn-primary btn-block'][contains(text(),'Continue')]
you can use this. It will always click on the button with the text Continue.
driver.findElements(By.tagName("button")).stream().filter(e -> e.getText().equals("Continue")).findFirst().get()
.click();
Option# 1- you can simply look for a button with text - continue if it is unique on your page.
"//button[contains(text(),'Continue')]"
Option# 2: just combine the ids using the OR operator like this,
"//div[#id='preferences' or #id='accountInfo' or #id='personalInfo']//button[contains(text(),'Continue')]"

Selecting from 2 Radio buttons when 1 button has no id - Webdriver

I am trying to write a script in web driver and part of the page that I am testing has 2 radio buttons where the person has to agree that something is correct.
By default the no button is selected and the user has to select the yes radio button to continue.
I have read a number of solutions for things similar and I've tried numerous different things but still can't get Selenium to select the yes radio button. It is a Yes/No question. The No field is already pre-populated but I need it to select Yes.
As you can see below there is a small amount of code for this part of the form.
<pre>
<form action="">
<input id="selfCert" name="selfCert" value="no" checked="checked" type="radio">No
<input name="selfCert" value="yes" type="radio">Yes
</form>
</pre>
I have tried to use the xpath and the css but the test just terminates.
Any advice would be greatly appreciated.
Thanks
Gareth
Can u use xpath to select the Yes radio button?
As both input fields share the same name="selfCert" I think u need to use xpath then.
I have an example in C# for Selenium, if you're using a different language you will need to modify it.
YesRadioButton = webDriver.FindElement(By.XPath("//form/input[2]"));
YesRadioButton.Click();
I have managed to resolve the issue now using a wait command until webdriver can see the button
WebElement element = driver1.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/div[5]/div[1]/div/form/input[2]"));
WebDriverWait wait = new WebDriverWait(driver1, 20);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();

How to get the xpath for the span button

I am new to selenium IDE and trying to get the target in the right order.
I have tried many combination to get the right element when there is span for the button. I need to get the xpath for the "Read More" button.Can someone please advise how the target in the IDE should be.
Here is the code:
<div class="is_centered l_bottom_pad">
<a class="btn_teal_outline has_arrow" href="https://test.com/jobs/view.php?id=8">
<span>Read More</span>
</a>
</div>
In some browsers (I think it was mainly MSIE) it is necessary to address the <a> element, not its child <span> in order to click a button or link. So you should adress:
//a[span[text()='Read More']]
Or you go directly for LinkText ("Read More") instead of XPath!
Targeting for span button is very simple. Just see what is unique attribute in that element.
I feel the button text itself is unique.Try this one
xpath=.//span[text()='Read More']