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

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

Related

Unable to click on an element using Selenium

<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);

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.

Get id where style equals x - Java - Selenium WebDriver

When I open a page, there is code that looks like the following:
<div id="policySetup_content">
<div id="bCS_insureds_contentWrap" style="display: none;">
<div id="bCS_policy_contentWrap" style="display: block;">
<div id="bCS_risks_contentWrap" style="display: none;">
<div id="bCS_rating_contentWrap" style="display: none;">
<div id="bCS_billing_contentWrap" style="display: none;">
<div id="bCS_attachments_contentWrap" style="display: none;">
<div id="bCS_submit_contentWrap" style="display: none;">
</div>
How would I go about getting the #id of whichever one is set to (style="display: block;) inside the #id policySetup_content?
The reason for this is so I can know which page I'm on (because it can be any one of them for various reasons). I need to know the page in order to know which Wrap id to use when working with elements.
Judging by this previou SO question you should be able to use the CSS Selector (div[style*="display:block"]), something along the lines of the below (untested).
String id = driver.findElement(By.cssSelector("div[style*=\"display:block\"]").getAttribute("id");
Because Selenium will not interact with elements that are not visible, you should be able to pull all the DIVs under the parent DIV and only get the one that is not hidden. I've never tried this approach before but I think it will work...
String id = driver.findElement(By.cssSelector("#policySetup_content > div[id]")).getAttribute("id");
BTW, if you aren't familiar with CSS Selectors this reads find an element with ID (#) policySetup_content that has an immediate child (>) DIV that has an ID. This may need to be tweaked depending on the real HTML that you are dealing with. If it doesn't work, let me know and I can try to help tweak it.
CSS Selector reference

How would I click this image link in Selenium when there is no id?

I am trying to click to go to where the href is referencing. I am not sure what code I would accomplish this with since there is no apparent id. Keep in mind the link referenced by href can change at any time.
<ul style="display: block; max-height: 230px; width: 129px; margin: auto; transition: all 500ms ease-in-out 0s;" id="pubCarousel" class="elastislide-list"><li style="width: 100%; max-width: 129px; max-height: 230px;">
<a href="/MTR/FBSC/en/36548659-040e-4185-9ad7-c9472d8fc36e/Page?storeId=bfd80755-bbfa-49b4-92ac-2f62ee2d1f83&postalCode=P3E3Z9">
<img src="http://mtr.ca.flyerservices.com/cached_images/pages/FBSC_45e00411-b791-44f7-8310-a77dc39d4fbd_111_53284_FB_E_01_B1_V1_E18.p1.jpg" alt="" border="0">
<p class="caption">
<span class="title">Version 1</span>
<br>
<span>July 16, 2015 to July 22, 2015</span>
</p>
</a>
</li></ul>
As skandigraun said in his comment there are other ways to find elements using Selenium. I have often had to use a CSS selector as well as xpath selectors.
For finding the CSS or xpath you need for the selector I usually use chrome developer tools or firebug to right click -> "Inspect element" followed by a right click on the desired element -> "Copy CSS path" or "Copy XPath"
driver.findElement(By.cssSelector("#pubCarousel li a img")).click();
or for XPath
driver.findElement(By.xpath("//*[#id=\"pubCarousel\"]/li/a/img")).click();
I am unfortunately away from my computer so I can't test them out right now. The XPath one should work for you, but I'm not sure if the CSS one will without a little tweaking.

How to access a specific child div using xpath? (Selenium Java)

I have the following html code:
<div class="panel">
<div class = "heading">
<span class="wName">Name</span>
<div class="foo1" style="display: none;"></div>
<div class="foo2" style="display: none;"></div>
</div>
</div>
I already located element panel and I'm trying to test when foo2 doesn't appear with the following line of code:
if (panel.findElement(By.xpath("../div[#class='foo2']")).getCssValue("display").equals("none"))
I'm not sure why this won't retrieve the element properly.
Your XPath is wrong! .. means "parent of". Single dot . would mean relative to current location.
Try: panel.findElement(By.xpath(".//div[#class='foo2']")
How about you use descendant
panel.findElement(By.xpath("//div[#class='panel']/descendant::div[#class='foo2']"));
Source http://www.caucho.com/resin-3.1/doc/xpath.xtp#descendant

Categories