I need relative xpath of the span element
<span class="icon"></span>
in selenium web driver.
<span class="custom-checkbox">
<input id="personalization1" name="personalization-terms-check" class="personalization-terms-check required" value="accept" type="checkbox">
<label for="personalization1">
<span class="icon"></span>
<span class="order-ready-text">yes! I double-checked all personalization entered above and i'm ready to order</span>
</label>
</span>
need relative xpath of the span element
<span class="icon">
in above html and I am getting relative path:
//*[#id="main"]/div[3]/div[4]/div[2]/div/span/label/span[1].
Please help me to get an relative xpath or another way to make an click on the span
NOTE:
<span class="icon">
It can be multiple so I need unique relative xpath.
You can wait for the element visibility before performing any action like below.
WebElement element=driver.findElement(By.xpath("//*[#name='personalization-terms-check']/following-sibling::label/span"));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();
and you can use different xpath or id to locate the element. the above xpath map be unique if only one personalization term checkbox on the page.
Can you try with this xpath?
"//label[#for='personalization1']/span[1]"
Hope this helps. Thanks.
Try this:
Try this XPath -
//input[#id='personalization1']/label/span
Try this xpath to click on checkbox:
.//input[#id='personalization1']
or you can use this xpath
.//span[contains(text(),'yes! I dou')]
Related
Using Selenium Webdriver and Java, I want to select a option from the list which is displayed inside 'input' tag instead of 'Select' tag.
Please find below details-
The GUI is as following-
Please find the GUI image here
<td nowrap="" valign="middle" colspan="3" rowspan="1">
<div class="mceGridField siebui-value mceField">
<input type="text" name="s_6_2_158_0" value="" aria-labelledby="BGC_Type_Label" aria-label="Type" style="height: 24px; width:80px;" class="siebui-ctrl-select siebui-input-popup siebui-align-left siebui-input-align-left ui-autocomplete-input" aria-describedby=" s_6_2_158_0_icon" maxlength="30" tabindex="0" role="combobox" autocomplete="off" data-seq="0" aria-readonly="false">
<span class="siebui-icon-dropdown applet-form-combo applet-list-combo" id="s_6_2_158_0_icon" data-allowdblclick="true"></span></div>
</td>
The GUI List of options are as following-
Please find the GUI image of option list after clicking
I am using below code to select one of the option from the list-
driver.findElement(By.xpath("//*[#id='s_6_2_158_0_icon']")).click();
But still I am not able to select any option.
Can you please help ?
To invoke click() on the desired option from the list you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name^='s_'][aria-labelledby='BGC_Type_Label'][aria-label='Type']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(#name,'s_')][#aria-labelledby='BGC_Type_Label' and #aria-label='Type']"))).click();
Did you try sendKeys method btw-
driver.findElement(By.xpath("//*[#id='s_6_2_158_0_icon']")).sendKeys("Fix");
And it's better to use the id -
driver.findElement(By.name("s_6_2_158_0_icon")).sendKeys("Fix");
This is my sample html code.
<div class="content">
<M class="mclass">
<section id="sideA">
<div id="mainContent">
<div class="requestClass">
<span>Check</span>
<input type="text" id="box">
</div>
</div>
<section>
<section id="sideB">
...
<section>
</M>
</div>
I want to set some value to my text field ("box"). So I tired to set like below code
driver.findElement(By.xpath("...")).sendKeys("SetValue");
My Xpath id is correct, it's exist in the page but am getting this error
no such element: Unable to locate element: {"method":"xpath","selector":"id("..."}
Why I am getting this error because of my custom tag,if yes how to get element inside custom tag?
As per the HTML you have provided to fill in some value to the text field represented by <input type="text" id="box"> you can use either of the following line of code:
cssSelector :
driver.findElement(By.cssSelector("section#sideA input#box")).sendKeys("SetValue");
xpath :
driver.findElement(By.xpath("//section[#id='sideA']//input[#id='box']")).sendKeys("SetValue");
If you still want to use XPath. This worked for me-
driver.FindElement(By.XPath(#"//*[#id='box']")).SendKeys("AB");
I don't think the custom tag causes any problem as the CssSelector also works-
driver.FindElement(By.CssSelector(#"m[class='mclass'] input")).SendKeys("AB");
You can use ID or xpath to locate it, My suggestion you have to use ID. Also use Explicit wait till element to be visible.
Using ID, your code is like this:
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
elem.sendKeys("test");
You can also use JavascriptExecutor
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='test';", elem);
I am struggling with Selenium.
Basically I want to click on the following element to toggle the element:
<div class="btn-group pull-right">
<a class=" mr-2" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown-ddd" href="javascript:void(0)" onclick="$(this).closest('.btn-group').toggleClass('open');"
<svg class="">
<use xlink:href="resources/icons/settings.svg#Layer_1" xmlns:xlink="http://www.w3.org/1999/xlink">
</svg>
</a>
It changes to:
<div class="btn-group pull-right open”>
<a class=" mr-2" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown-ddd" href="javascript:void(0)" onclick="$(this).closest('.btn-group').toggleClass('open');">
<a class=" mr-2" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown-ddd" href="javascript:void(0)" onclick="$(this).closest('.btn-group').toggleClass('open');">
<svg class="">
<use xlink:href="resources/icons/settings.svg#Layer_1" xmlns:xlink="http://www.w3.org/1999/xlink">
</svg>
</a>
The method I wrote for clicking this element:
WebElement dropDown = driver.findElement(By.xpath("//div[#class='btn-group.pull-right’]”));
dropDown.click();
However, I am getting an Exception:
no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn-group.pull-right']"}
Any recommendations on why I am geting this exception?
You can use 'or' logical operator in ur xapth which locate both.
Try this Xpath :-By.xpath("//div[#class='btn-group.pull-right’|//div[#class='btn-group pull-right open’ "]
your html shows element has class "class="btn-group pull-right" "
and your xpath "By.xpath(" //div[#class='btn-group.pull-right’]”" if i closely look at it your are adding dot in the xpath to remove space which is not required when using xpath, spaces will be removed by dot in css. So try using this xpath
//div[#class='btn-group pull-right’]
I am trying to get the text under the tag. That is Arduus, Gstaad, Switzerland. I tried with the classname and also with xpath.
driver.findElement(By.className("chalet-details__title"));
String chaletTitle = driver.findElement(By.xpath("//div/h1[#class='chalet-details_title']")).getText();
But its giving NoSuchElementException. The classname has double underscore(__) .Is it because of that not working? Can anyone help me with this?
<div class="col-md-12 col-sm-12 col-xs-12">
<h1 class="chalet-details__title">
<span class="chalet-details__title-property">Arduus</span>,
<a class="chalet-details__title-resort" href="/ski- resorts/switzerland/gstaad">Gstaad</a>,
<a class="chalet-details__title-country" href="/ski- resorts/switzerland">Switzerland</a>
</h1>
<div class="chalet-details__rating">
<div class="chalet-details__wrapper">
<span class="chalet-details__star" style="width: 108px;"></span>
<span class="chalet-details__mask"></span>
</div>
</div>
</div>
You can try something like:
driver.findElement(by.cssSelector("h1.chalet-details__title"))
I just tried this and it worked fine with the provided html, if this still fails for you, can you verify that there aren't any iframes on the page.
Both your locators are OK, but you might need to wait for your element to be present in DOM:
WebDriverWait wait= new WebDriverWait(driver,20 );
String chaletTitle = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("chalet-details__title"))).getText();
Another reason for NoSuchElementException: your element could be located inside an iframe, so you need to switch to that frame before handling target element:
driver.switchTo().frame("iframeNameOrId");
If it has no attributes as id or name:
WebElement someFrame = driver.findElement(By.xpath("//iframe[#someAttr='someValue']"));
driver.switchTo().frame(someFrame);
To get all the text under h1 tag find all elements using locator as follows:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *")); //it will find all elements immediately under the h1 tag
Now, iterate over the elements.
Complete code:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *"));
for(WebElement item : items){
System.out.println(item.getText());
}
How to locate the element below in Selenium?
<input id="mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R" class="iceSelInpTxtTxt fieldTxt" type="text" value="" style="width: 150px;" onmousedown="this.focus();" onfocus="setFocus(this.id);svOnFocus(formOf(this), this, event, false);" onblur="setFocus('');svOnBlur(formOf(this), this, event);" name="mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R" autocomplete="off">
I tried with ends-with, it did not work.
You can use the below
WebElement inputClass = driver.findElement(By.className("iceSelInpTxtTxt fieldTxt"));
WebElement Element = driver.findElement(By.xpath("Try any Below xpaths"))
If your input id is unique then use below xpath
//input[#id='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R']
OR
//input[#name='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R']
OR if the combination of both of id and name make them unique then use below xpath
//input[#name='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R' and #id='mainForm:field_LayFact450505264_16032015_145612--Order-ServiceClass_R']
OR
//input[#class='iceSelInpTxtTxt fieldTxt']
Hope it will help you :)