How to find an xpath of element? - selenium

I am trying to find the xpath of svg next to
"//label[#class='btn btn-light p-1']/input[#value='star']
https://9qrcode.com/#link
then select Design

I hate to do such xpaths, since I agree with Eliyahu - these kind of xpaths won't be unique...but since you asked, here you go, this should work for you:
//input[#value = 'star']/parent::label[contains(#class, 'btn-light')]/preceding-sibling::label[contains(#class, 'btn-light')]/*[name() = 'svg']

You can use this xpath -
(//label[contains(#class,"btn btn-light")]/*[local-name() = 'svg'])[2]
your can put your element number in replacing [2].

//input[#value='star']/preceding::label[#class='btn btn-light p-1'][1]
This will get the preceding element before the value of star which since it's not unique will give two values.

Related

How to create Dynamic Xpath for a Tag ? Selenium

Im trying to create 1 dynamic xpath for all 4 a tag which i need store in a List to perform actions. Tomorrow it might be more Locators will be like that.
"//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td[1]/a"
"//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td[2]/a"
"//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td[3]/a"
"//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td[4]/a"
Need help to create dynamic xpath for that. Thank you all.
You could try something like this
row = 1
"//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td[" + row + "]/a"
Then you could just increment/loop over "row" however you wish
use findelements with below locator
"//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td/a"
it finds all td[*]/a elements
First 4.
//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td[position()<5]/a
All td in all trs.
//*[#id=\"ctl00_GridView\"]/tbody/tr[17]/td/table/tbody/tr/td/a

xpath - upper case and lower case button text

I have multiple ok buttons in my application in the combinations: OK, ok, oK and Ok. How can i write a single #findby expression to identify all of them with the one webelement.
Code example
<button type="button">OK</button>
Here is the other solution in pure xpath 1.0 using xpath functions.
//button[contains('OK,ok,Ok,oK',text())][string-length('OK')=2]
or
//button[contains('OK,ok,Ok,oK',text())][string-length(text())=string-length('OK')]
Edit: Simple approach using translate
//button[translate(text(),'ok','OK')='OK']
You can specify matching text with xpath:
//button[text()='OK']
In your case, to match them all:
//button[text()='OK' or text()='oK' or text()='ok' or text()='Ok']

Webdriver click second element in list

The page I'm testing has 2 elements with the same name and I need to click the second Element.
I can get the elements by using:
driver.findElements(By.linkText("Services"));
But I don't know how to click on the second element.
There are two ways to do this:
1) Using xpath, try in following manner.
driver.findElement(By.xpath("('xpath of the link')[2]"));//If you had given html, I could have added exact xpath.
2) Using findElements() you can try following:
List<WebElement> li = driver.findElements(By.linkText("Services"));;
li.get(1).click();//If there are only two such element, here 1 is index of 2nd element in list returned.
Hope you get the idea. :)
Or for short you can do
driver.FindElements(By.ClassName("drop"))[1].Click();
If you`re using python, you can try this way:
li = driver.findElements(By.linkText("Services"))[index].click()

one drop down two ids(div id,select id) how to select in selenium webdriver in java

html
false;" href="#"> Choose One
class="select2-search-choice-close" style="display:none;"/>
Choose One
Andhra Pradesh
You need one id to find an element, use select's id.
new Select(driver.findElement(By.id("tx_rm0x0_state"))).selectByVisibleText("Andhra Pradesh");
It should work just fine.
I think in your code ids are dynamic and may it would change every time on page load.
So it would be better if you can find the element based on tagname:
new Select(driver.findElement(By.tagname("select"))).selectByVisibleText("Andhra Pradesh");
or you can also use the xpath to find the element.
Use this
new Select(driver.findElement(By.id("s2id_tx_rm0x0_state")).selectByVisibleText("Andhra Pradesh");
OR Best Solution is:
Select select = new Select(driver.findElement(By.id("s2id_tx_rm0x0_state")));
select.selectByValue("Andhra Pradesh");
Enjoy!

Xpath for href element

I need to click on the below href element,which is present among similar href elements.
<a id="oldcontent" href="listDetails.do?camp=1865"><u>Re-Call</u></a>
Can anyone provide me xpath to click the above href link?
Try below locator.
selenium.click("css=a[href*='listDetails.do'][id='oldcontent']");
or
selenium.click("xpath=//a[contains(#href,'listDetails.do') and #id='oldcontent']");
This works properly try this code-
selenium.click("xpath=//a[contains(#href,'listDetails.do') and #id='oldcontent']");
This will get you the generic link:
selenium.FindElement(By.XPath("xpath=//a[contains(#href,'listDetails.do')")).Click();
If you want to have it specify a parameter then you will have to test for each one:
...
int i = 1;
selenium.FindElement(By.XPath("xpath=//a[contains(#href,'listDetails.do?camp=" + i.ToString() + "')")).Click();
...
The above could utilize a for loop which navigates to and from each camp numbers' page, which could verify a static list of camps.
Please excuse if the code is not perfect, I have not tested myself.
have you tried:
//a[#id='oldcontent']/u[text()='Re-Call']
for me worked //a[text()='Re-Call']
what worked for me:
//a[contains(#href,'logout')]
Below works fine.
//a[#id='oldcontent']
If you've tried certain ones and they haven't worked, then let us know, otherwise something simple like this should work.
Best way to locate anchor elements is to use link=Re-Call:
selenium.click("link=Re-Call");
It will work..