Unable to click on an element using Selenium - java

<div id="_PHYLINSPortlet_WAR_PHYLINSPortlet_INSTANCE_o3P5_:form_PolicyContent_UI2:flatQuoteMenuBar:j_id7:VEHICLES" class="iceMnuBarItem portlet-menu-cascade-item">
<a class="iceLink" href="javascript:;" onclick="return Ice.Menu.cancelEvent(event);">
<span class="iceMnuBarItemLabel">Vehicles (1)</span>
</a>
</div>
I have to click on vehicles (1) menu
I tried lots of xpath selectors. gettext() is working but .click is not.
//driver.findElement(By.xpath("//*[#id='_PHYLINSPortlet_WAR_PHYLINSPortlet_INSTANCE_o3P5_:form_PolicyContent_UI2:flatQuoteMenuBar:j_id7:VEHICLES']/a/span")).click();
my 2nd xpath:
//driver.findElement(By.xpath("//span[(#class='iceMnuBarItemLabel') and contains (text(),'Vehicles')]")).click();

I guess issue is inside your html due to javascript was not defined properly.
Your html code see the below line of code.
<a class="iceLink" href="javascript:;" onclick="return Ice.Menu.cancelEvent(event);">
After modified your html see the below line of code.
<a class="iceLink" href="#" onclick="return Ice.Menu.cancelEvent(event);">
Now try to click on webelement using anyone of these below method.
Try this way to click vehicle element.
driver.findElement(By.xpath("//a/span[contains(text(), 'Vehicles (1)')]")).click();
OR
Click Vehicle element using Java-script executor.
WebElement vehicle = driver.findElement(By.xpath("//a/span[contains(text(), 'Vehicles (1)')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", vehicle);

Related

#FindBy Selenium how to find ::before

::before
I have following structure on site and ::before is checkbox where I'm supposed to click, found some topics, but there was no #FindBy annotation. Is it possible to find this ::before in code?
<span class="jJ">
<label class="kJ" data-test-id="checkbox_bonus_card">
<input class="mJ" type="checkbox" name="hasBonusCard" value="">
<span data-test-id="checkbox_bonus_card_text" class="lJ">
::before
</span>
</label>
</span>
I tried #FindBy(xpath = "//label[#data-test-id='checkbox_bonus_card'//span['::before']") but it didn't work.
If I use something like this:
#FindBy(xpath = "//input[#name='hasBonusCard']")
WebElement checkboxBonusCard;
Error appears:
Caused by: ElementClickInterceptedException: element click intercepted: Element <input class="mJ" type="checkbox" name="hasBonusCard" value=""> is not clickable at point (568, 78). Other element would receive the click: <span class="jJ">...</span>
I think it's in input field that you wanna interact with :
#FindBy(name = "hasBonusCard")
WebElement bonusCard;
:: basically means this :
In CSS, ::before creates a pseudo-element that is the first child of
the selected element. It is often used to add cosmetic content to an
element with the content property.
More :
Since ::after & ::before are a pseudo element which allows you to
insert content onto a page from CSS (without it needing to be in the
HTML). While the end result is not actually in the DOM, it appears on
the page as if it is - you see it but can't really locate it with
xpath for example
Read more here

I am unable to click on Button element through my code, could anybody help here

I would like to be able to click on Button through my selenium script using Java.
I tried to do it multiple ways by class, and index in xpath:
1) driver.findElement(By.xpath("contains(#class,'btn btn-alt btn-small tooltip-element') and contains(#tabindex,'0')")).click();
2) driver.findElement(By.xpath("//*button[#tabindex=0]")).click();
My html path is as below:
<a href="#" class="btn btn-alt btn-small tooltip-element" tabindex="0" role="button" data-placement="bottom" aria-label="Help" ng-click="ecdapp.uploadBlueprintModalPopup()" aria-expanded="true" aria-describedby="tooltiptextBtn"> Create <span class="arrow"></span>
</a>
Try this code:
driver.findElement(By.linkText('Create')).click();
It would seem that the link in question is part of a tooltip. Tooltips are only visible when a mouse cursor is hovered over its corresponding web element.
You'll need to hover the cursor over the element in question, then you should be able to locate the link and it should be clickable.

Selenium (Java) message "Unable to locate element" but element and XPath exist

I am new to Selenium WebDriver, using NetBeans Java and Firebug for Firefox to get XPath.
The problem is even though some elements are visible and clickable in the browser, and Firebug finds the XPath expression, I still get an error "Unable to locate element". The problem is, the element is not in a frame (there aren’t any frames), not in another window, it's visible and clickable and it has a clear XPath expression.
I used Wait and Thread.sleep, but it didn't help.
Also, I tried all solutions I could find on Stack Overflow similar to this matter.
The element is inside of the table which has some "div" elements inside. This is the XPath expression:
.//*[#id='NavigationRadPanelBar_i2_i0_trvStandardView']/ul/li[3]/div/span[3]
It seems that the panel is a problem, because it can not locate the panel too. I tried another element inside the same panel which has a title and text with this:
driver.findElement(By.xpath("//*[contains(text(),'Technical Attributes')]")).click();
But still I got same message. Similar for all elements inside this panel and panel itself. It seems whatever I tried, it's unable to locate. Everything outside of this panel is working fine with drivers.
The page has a left panel where these elements are located. Some of them are nodes that can be expanded, and some are just links, but nothing can be located.
What can be the cause of element that is obviously existing and visible, but still cannot be located by WebDriver?
I tried to locate the main panel:
driver.findElement(By.xpath(".//*[#id='NavigationRadPane']")).click();
but still the same exception.
This is part of the HTML content (it's very long):
<div id="RAD_SLIDING_PANE_CONTENT_navigatiionRadSlidingPane" class="rspSlideContent" style="overflow: hidden; width: 200px; height: 579px;">
<div id="NavigationRadPanelBarPanel" style="display: block;">
<div id="NavigationRadPanelBar" class="RadPanelBar RadPanelBar_Office2007" postback="false" style="background-color:GhostWhite;height:100%;width:100%;">
<ul class="rpRootGroup">
<li class="rpItem rpFirst">
<li class="rpItem">
<li class="rpItem rpLast">
<a class="rpLink rpExpandable rpExpanded" href="#">
<div class="rpSlide" style="display:block;">
<ul class="rpGroup rpLevel1 111460" style="display: block; height: 274.731px; width: 100%;">
<li class="rpItem rpFirst rpLast">
<div class="rpTemplate">
<div id="NavigationRadPanelBar_i2_i0_trvStandardView" class="RadTreeView RadTreeView_Office2007">
<ul class="rtUL rtLines">
<li class="rtLI rtFirst">
<li class="rtLI">
<li class="rtLI">
<div class="rtMid">
<span class="rtSp" />
<span class="rtPlus rtPlusHover" />
<img class="rtImg" src="..." alt="Technical Attributes" />
<span class="rtIn" title="Technical Attributes">Technical Attributes</span>
</div>
<ul class="rtUL" style="display:none;">
</li>
<li class="rtLI rtLast">
Use:
driver.switchTo().activeElement();
This way, the driver will switch to the panel you have clicked. Then you can perform the rest of the operations you want in the panel.
Here is the answer to your question:
The error says it all, Unable to locate element, because the XPath expression seems incorrect to me.
If you want to click on the element with title set to Technical Attributes you can consider to try with the following XPath expressions:
//div[#id='NavigationRadPanelBar_i2_i0_trvStandardView']/ul/li/li/li/div/span[#class='rtIn']
or
//div[#id='NavigationRadPanelBar_i2_i0_trvStandardView']/ul/li/li/li/div/span[#title='Technical Attributes']

XPath of an element in HTML code

I am trying to click on 'Select' button embedded in following HTML code using selenium webdriver-java. For this I am trying to write XPath for the 'select' button.
<div class="product eq-height" style="padding-bottom: 0px ! important; min-height: 329px;">
<h3 class="h4">BYOX Samsung S6 White</h3>
<input name="productModel" value="BYOX Samsung S6 White" type="hidden">
<div class="subtitle">
<span>Samsung</span>
</div>
<a class="select-device-button" href="javascript:void(0);">
<div class="item">
<img alt="" class="prod-img-lrg lazyloaded" data-src="https://vgeco-oat1.vodafone.com/images/Samsung_Galaxy_S6_White.jpg" src="https://vgeco-oat1.vodafone.com/images/Samsung_Galaxy_S6_White.jpg"> <noscript><img alt="" src="https://vgeco-oat1.vodafone.com/images/Samsung_Galaxy_S6_White.jpg"></noscript>
</div>
</a>
<div class="info">
<div class="inner">
<ul class="cost">
<li><b>Total cost</b></li>
<li class="price">£ 700.00</li>
</ul>
<ul class="cost">
<li><b>My cost</b></li>
<li class="price">£ 200.00</li>
</ul>
</div>
<div class="cta">
<a class="btn btn-sml select-device-button" href="javascript:void(0);">Select</a>
</div>
</div>
<div class="co-link-wrap txt-center"></div>
</div>
what would be the xpath to fetch the Select button in above code. the field: BYOX Samsung S6 White can be different in every piece of code. so i need a generic code to fetch the select element. I am trying to select the button based on the string: BYOX Samsung S6 White and there are a number of different product available on the webpage with associated Select button.
There is a simple locator that fits this use case perfectly:
driver.findElement(By.linkText("Select")).click();
Note that instead of driver in this case, there can be a WebElement used, for example, imagine you've already found a product and want to "select" it:
WebElement product = driver.findElement(By.cssSelector("div.product"));
product.findElement(By.linkText("Select")).click();
I need to click the 'Select' button only if the product name is BYOX Samsung S6 White in above div class. how can i do this ?
Sure:
WebElement product = driver.findElement(By.xpath("//div[contains(#class, 'product') and h3 = 'BYOX Samsung S6 White']"));
product.findElement(By.linkText("Select")).click();
There are two select elements (one is the big image the other the Select-link)
Do you mean the second element?
Xpath:
//div[#class='cta']/a[#class]
Css:
div.cta a.select-device-button
(similar but more efficient in Selenium and in my opinion easier to declare)
If you wanted the first element in both cases just leave out the declaration for the div element.
In Firefox you can easily try out locators and it is always a bit depending how strict or flexible you want to keep them.
To the edited point:
You might check independently for the value of the hidden input field (it seems here is no parsing necessary) and is not dependent on the localisation. Alternatively check the header element or the image reference.
You don't have to use xpath. Simpler way will be using other locators. You can use
className
WebElement button = driver.findElement(By.className("btn"));
WebElement button = driver.findElement(By.className("btn-sml"));
WebElement button = driver.findElement(By.className("select-device-button"));
linkdText
WebElement button = driver.findElement(By.linkText("Select"));
cssSelector
WebElement button = driver.findElement(By.cssSelector(".btn.btn-sml.select-device-button"));
Each one of those can give you the button element.

Click on Tabs in Selenium webdriver

I am trying to open different section of page. These Section will open on click of different tabs.
Below is HTML Structure of Page
<div id="MainContentPlaceHolder_divMainContent">
<div id="MainContentPlaceHolder_tbCntrViewCase" class="Tab ajax__tab_container ajax__tab_default" style="width: 100%; visibility: visible;">
<div id="MainContentPlaceHolder_tbCntrViewCase_header" class="ajax__tab_header">
<span id="MainContentPlaceHolder_tbCntrViewCase_tbPnlCaseDetails_tab" class="ajax__tab_active">
<span id="MainContentPlaceHolder_tbCntrViewCase_tbPnlVehicle_tab" class="ajax__tab_hover">
<span class="ajax__tab_outer">
<span class="ajax__tab_inner">
<a id="__tab_MainContentPlaceHolder_tbCntrViewCase_tbPnlVehicle" class="ajax__tab_tab" style="text-decoration:none;" href="#">
<span>Vehicle</span>
</a>
</span>
</span>
</span>
and I have Written Below Lines but these are not working
driver.findElement(By.id("__tab_MainContentPlaceHolder_tbCntrViewCase_tbPnlVehicle")).click();
driver.findElement(By.xpath("//a[text()='Vehicle']")).click();
I got Source Not Found Error
As per the OP's comments, I am posting the xpaths that can be used to locate the concerned element :
1- //span[#id='MainContentPlaceHolder_tbCntrViewCase_tbPnlVehicle_tab']//span[.='Vehicle']
This will locate the span element with innerHTML/text as Vehicle which is a descendant of span with id MainContentPlaceHolder_tbCntrViewCase_tbPnlVehicle_tab
OR
2-//span[#id='MainContentPlaceHolder_tbCntrViewCase_tbPnlVehicle_tab']//span[.='Vehicle']/..
This will locate the parent of span element with innerHTML/text as Vehicle which is a descendant of span with id MainContentPlaceHolder_tbCntrViewCase_tbPnlVehicle_tab which in this case is an a element.
Please check if this works for you. Else, let me know how many matching nodes does it show, when you use them. We will sort this one out.

Categories