Toggle the element - Selenium - selenium

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’]

Related

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="card-id-oidc-i"]/a"}

I'm having this strange error (sometimes works sometimes does not):
no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="card-id-oidc-i"]/a"}
My button has this:
<a href="/auth/realms/Global-Realm/broker/id-oidc-i/login?client_id=web&tab_id=Doz54nelUC0&session_code=gwAePmGfpQ2hBLommJO7Rswc1gNkB90Ctc4">
<div style="width:100%;height: 40px;">
<span class="arrow arrow-bar is-right"></span>
</div>
<div class="image" style="background-repeat: no-repeat;margin:auto; width:115px;height:120px"></div>
<div style="margin-top: 10px;min-width:170px">
<h4 style="text-align:center;"><b>log in</b></h4>
</div>
</a>
It's XPATH is:
//*[#id="card-id-oidc-i"]/a
I did this:
driver.findElement(By.xpath("//*[#id=\"card-id-oidc-i\"]/a")).click();
It is strange because sometimes works just fine but sometimes it fails.
Why?
You probably missing a delay.
Try using this:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='card-id-oidc-i']/a"))).click();
BTW your locator is based on some parent element with id = card-id-oidc-i while you shared here only the child a element HTML.
Also, no need to put \ before " inside a String. You can simply use ' instead as I do here.
NoSuchElementException error may occur when :
HTML element may not be present in a DOM yet. So you have to implement WebDriverWait to wait until element is present and visible in a web page.
HTML element may not be inside frame or iframe.
Maybe in your case it is not in the DOM yet, try to wait until it is visible and on clickable position.

Selenium can't click element obscure by "overlay" element

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <select id="buttonmodule" class="x2j" name="buttonmodule"> is not clickable at point (437,425) because another element <div id="overlay" class="r1_hide_busy_status r1_show_busy_status"> obscures it
Message: Element <select id="buttonmodule" class="x2j" name="buttonmodule"> is not clickable at point (437,425) because another element <div id="overlay" class="r1_hide_busy_status r1_show_busy_status"> obscures it
I tried to find element id="buttonmodule" to click but I got message that it is not clickable by overlay element
XPATH of buttonmoduel: "//a[contains(text(),'buttonmodule')]
info of overlay: <div id="overlay" class="r1_hide_busy_status" style="height: 967px; width: 1853px;" xpath="1"></div>
According to my perception, your selected element is a dropdown developed using the select tag. Basically dropdown is developed using select tag will work by using selectByVisibleText() or selectByValue() or selectByIndex() methods as these are default methods to select values in the dropdown.
And we need to locate the dropdown and pass that object reference as an argument for select class nothing but associating both.
For example:-
Webelement dd =
driver.findElement(By.xpath("//div[#id='buttonmodule']"));
Select s=new Select(dd);
s.selectByVisibleText("2");

I am trying to figure out the XPath which will retrieve the img alt text from the following HTML

I am trying to figure out the XPath which will retrieve the img alt text from the following HTML
<li class="m-navbar-item bottom-border float-left m-navbar-thumbnail">
<img alt="Always There">
</li>
To get the alt text of a element, you need to use selenium web-driver API get attribute. You can locate the element using x-path. Your requirement may be as given below.
List<WebElement> lstItem = driver.findElements(By.xpath("//li[conatains(#class,'m-navbar-item')]/img");
for(WebElement item:lstItem){
System.out.println(item.getAttribute("alt"));
}

Not able to identify custom checkbox element/Need relative xpath

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')]

Using Selenium to select text

Want to select the text "This is for testing selector" from below HTML code.
<div class="breadcrumb">
<a title=" Home" href="http://www.google.com/"> Home</a>
<span class="arrow">»</span>
<a title="abc" href="http://www.google.com/">test1</a>
<span class="arrow">»</span><a title="xyz" href="http://www.google.com/">test2</a>
<span class="arrow">»</span>
This is for testing selector
</div>
I'm not sure if there an easy way out for this or not. It turned out to be more difficult than I thought. Below mentioned code is tested locally and giving correct output for me ;)
String MyString= driver.findElement(By.xpath("//div[#class='breadcrumb']")).getText();
//get all child nodes of div parent class
List<WebElement> ele= driver.findElements(By.xpath("//div[#class='breadcrumb']/child::*"));
for(WebElement i:ele) {
//substracing a text of child node from parent node text
MyString= MyString.substring(i.getText().length(), MyString.length());
//removing white spaces
MyString=MyString.trim();
}
System.out.println(MyString);
Let me know if it works for you or not!
Try with this example :
driver.get("http://www.google.com/");
WebElement text =
findElement(By.className("breadcrumb")).find("span").get(1);
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
I suggest also that you copy the xpath for the text you need and put it here to have the exact xpath
You cannot select text inside an element using xpath.
Xpath can only help you select XML elements, or in this case, HTML elements.
Typically, text should be encased in a span tag, however, in your case, it isn't.
What you could do, however, is select the div element encasing the text. Try this xpath :
(//div[#class='breadcrumb']/span)[3]/following-sibling::text()
You could try Abhijeet's Answer if you just want to get the text inside. As an added check, check if the string obtained from using getText() on root element contains the string obtained from using getText() on the child elements.