How would one locate following object [ul, li] in selenium webdriver? - selenium

Could you help me in figuring out how to proceed with locating "new" using selenium webdriver in Java.
Code Snippet:
<ul class="ms-crm-CommandBar-Menu">
<li tabindex="-1" class="ms-crm-CommandBarItem ms-crm-CommandBar-Menu ms-crm-CommandBar-Button" title="New Create a new Account record." id="account|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.account.NewRecord" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid" style="white-space: pre-line; display: inline-block;"><span tabindex="-1" class="ms-crm-CommandBar-Button ms-crm-Menu-Label" style="max-width:200px"><a tabindex="0" class="ms-crm-Menu-Label" onclick="return false"><img tabindex="-1" class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" src="/_imgs/imagestrips/transparent_spacer.gif" style="vertical-align:top" alt="New"> <span tabindex="-1" class="ms-crm-CommandBar-Menu" style="max-width:150px" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid"> New </span> </a> </span> </li>

Disclaimer: I'm using C# but shouldn't make much difference.
The easiest would be using a CssSelector. It's a bit verbose in this case, but that doesn't matter:
driver.FindElement(By.CssSelector("span[command=\"account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid\"]"));
This will find the span element which contains nothing but the text you want. Assuming the command is unique on the page, otherwise you'll need to refine.
Alternatively you can use XPath to achieve the same:
driver.FindElement(By.XPath("//span[#command='account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid']");

Related

Unable to automate a click using Python and Selenium

I'm trying to automate adding requirements to a TestLink database. I'm running into an issue trying to click on this anchor/span.
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span>
</a>
Here is the section of Python code with things I've tried:
anchor = browser.find_element_by_xpath('//a[contains(#href, "REQ_SPEC_MGMT")]')
span = anchor.find_element_by_xpath('.//span')
anchor.click() # Doesn't work
span.click() # Doesn't work
browser.execute_script("arguments[0].click();", anchor) # Doesn't work
browser.execute_script("arguments[0].click();", span) # Doesn't work
I don't get any errors, but I still don't get the page that appears when I manually click on the link. I verified that I'm finding the correct anchor/span by dumping out the properties so I know I have the correct elements. I've also tried long pauses just to make sure that the element is clickable before I try it. Any ideas on what I am doing wrong? Thanks!
Update - Here's a larger section of the HTML:
<div id="tree_div" style="overflow:auto; height:100%;border:1px solid #c3daf9;" class=" x-panel x-tree">
<div class="x-panel-bwrap" id="ext-gen12"><div class="x-panel-body x-panel-body-noheader" id="ext-gen13" style="overflow: auto;">
<ul class="x-tree-root-ct x-tree-arrows" id="ext-gen14">
<li class="x-tree-node"><div ext:tree-node-id="17472" class="x-tree-node-el x-unselectable x-tree-node-expanded" unselectable="on" id="extdd-1">
<span class="x-tree-node-indent"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-end-minus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-2">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:TPROJECT_REQ_SPEC_MGMT(17472)" tabindex="1">
<span unselectable="on" id="extdd-3">ProjTasks (0)</span>
</a>
</div>
<ul class="x-tree-node-ct" style="">
<li class="x-tree-node">
<div ext:tree-node-id="17473" class="x-tree-node-el x-unselectable folder x-tree-node-collapsed" unselectable="on" id="extdd-4">
<span class="x-tree-node-indent"><img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-icon"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-plus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-5">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span></a></div><ul class="x-tree-node-ct" style="display:none;">
</ul>
</li>
At first, try to use implicit selenium wait, to make sure that all your elements is download.
anchor = WebDriverWait(browser,30).until(EC.element_to_be_clickable((By.XPATH, '//a[contains(#href, "REQ_SPEC_MGMT")]')))
span = WebDriverWait(anchor,30).until(EC.element_to_be_clickable((By.XPATH, './/span')))
If it does'n work fine, try to execut js script in href:
browser.execute_script("javascript:REQ_SPEC_MGMT(17473)")
or
browser.execute_script(anchor.get_attribute("href"))
I figured out the issue. Some of the characters I was passing to execute_script were being dropped by my vnc software (Python Selenium script drops keys but only when used on a VNC).
To click() on the element with text as 0:Project-0 (0) element is an JavaScript enabled element so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.x-tree-node-anchor[href*='REQ_SPEC_MGMT']>span[id^='extdd-']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='x-tree-node-anchor' and contains(#href, 'REQ_SPEC_MGMT')]/span[starts-with(#id, 'extdd-') and text()='0:Project-0 (0)']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

how to match text in a div element using xpath?

I am trying to locate an xml element in selenium using xpath. It is nested in inside .
I have numbers 3498546 and 3498755 in hand. I need to match these numbers to the numbers listed in elements and locate the specific .
I have tried using various combinations with below:
xpath=//*[#id="tabs"]/ul/li/[contains(div,'3498546')]
But it never worked.
Below is and example html code, I have around 100 listed in
<div id="tabs">
<ul>
<li class="unknown">
<span style="">DELIVERED</span>
<a title="A1" onclick="submitForm('e1:eForm',1);return false;"
class="eLink" href="#">
<div class="c1">"Year 2008
"
<br>"3498546
"
<br>
</div>
<strong>Date: </strong>05/14/2019
</a>
</li>
<li class="unknown">
<span style="">DELIVERED</span>
<a title="A2" onclick="submitForm('e1:eForm',1);return false;"
class="eLink" href="#">
<div class="c1">"Year 2008
"
<br>"3498755
"
<br>
</div>
<strong>Date: </strong>05/14/2019</a>
</li>
</ul>
</div>
I want to be able to locate and click the element which has the text 3498546 or 3498755.
Here is the xpath based on the 3498546.
//div[#class='c1'][contains(normalize-space(.),'3498546')]
you can change the 3498546 value to the required number and use the same. In case you want to get all the elements having numbers and click each one of them then you can use the below xpath.
//div[#id='tabs']//div[#class='c1']
To click() on either of the elements with text as 3498546 or 3498755 you can use the following solutions:
3498546:
Java & partialLinkText:
driver.findElement(By.partialLinkText("3498546")).click();
3498755:
Java & partialLinkText:
driver.findElement(By.partialLinkText("3498755]")).click();
//div[contains(text(),'3498755')]
All you need is to look for the div element that its text contains the numbers you're looking for.

Unable to locate click the check box

Please help me the locate the check box and select. There are multiple check boxes, there is no way to locate them uniquely.
Here is the Code for one of such check box.
Thanks in Advance for the Help!!!
<div class="col-xs-2 col-sm-2 col-lg-2" style="height:65px">
<ul class="list-inline pull-right">
<li>
<md-input-container class="md-block">
<md-checkbox value="$index+1check" class="checkbox ng-valid ng-dirty ng-touched ng-empty" ng-model="item.Selectedd" ng-click="toggle($index+1, selected,item.TitleId,item)" icon,md-checkbox.md-checked._md-icon="{background-color: green;}" id="Cbk_List" role="checkbox" tabindex="0" aria-checked="false" aria-invalid="false" style=""><div class="_md-container md-ink-ripple" md-ink-ripple="" md-ink-ripple-checkbox=""><div class="_md-icon"></div></div><div ng-transclude="" class="_md-label">
</div></md-checkbox>
</md-input-container>
</li>
<li>
<div class="manageTitle_CirclCard">
<div class="ng-binding">2</div>
</div>
</li>
</ul>
</div>
You should try to locate using cssSelector with it's attribute tabindex as below :-
#Cbk_List[tabindex='0']
Edited :- If checkbox element has not any attributes with unique value, you should try using xpath with index, assuming you want to get first checkbox element, then try below xpath :-
(.//*[#id = 'Cbk_List'])[1]
Note :- In above xpath, just change the index from 1 to desire one checkbox to find.

Dynamic search unable to show the result through selenium webdriver?

application consist dynamic search box &
while typing "Mumbai" search results drop down display as below,
But through selenium as,
driver.findElement(By.id("searchstr2")).sendKeys("Mumbai");
or
driver.findElement(By.id("searchstr2")).sendKeys("Mumbai"+ARROW_DOWN);
search result not display as,
text box html as,
<input id="searchstr2" class="search ui-autocomplete-input" type="text" placeholder="Search for Building, Location or Developer" autocomplete="off" name="searchstr2" size="35" style="background-image: none;"
after successful search list display as,
<li id="ui-id-117" class="ui-menu-item" tabindex="-1">
<a>
Nariman Point -
<b style="font-size:11px">
<span style="font-size:.8em; /*color:#EE7600;*/ color:#888888; float:right;">locality</span>
</a>
</li>
<li id="ui-id-118" class="ui-menu-item" tabindex="-1">
<li id="ui-id-119" class="ui-menu-item" tabindex="-1">
<li id="ui-id-120" class="ui-menu-item" tabindex="-1">
<li id="ui-id-121" class="ui-menu-item" tabindex="-1">
please suggest.
The problem with the autocomplete fields is that usually there is a Javascript event that waits for text to be present in the field in order to display the available suggestions (could be Ajax that gets executed to get the suggestions from the server). SendKeys doesn't trigger that event, so you could try to click into the field after you've entered you're text.
Basically:
WebElement suggestion = driver.findElement(By.id("searchstr2"));
suggestion.sendKeys("Mumbai");
suggestion.click();
I didn't test this, so you could try using the click() method before sendKeys().
If this doesn't fix your issue, you could try to do it by using Javascript to trigger your onChange event:
WebElement suggestion = driver.findElement(By.id("searchstr2"));
suggestion.click();
suggestion.sendKeys("Mumbai");
((JavascriptExecutor) driver).executeScript("$(arguments[0]).change(); return true;", suggestion);
java scripts takes some time to load the list so i have added timer in between send name & arrow down key event will be works for me code snap as,
WebElement ar=driver.findElement(By.id("searchstr2"));
ar.sendKeys("Mumbai");
Thread.sleep(2000);
ar.sendKeys(Keys.ARROW_DOWN);

How to Click on a Text in Selenium Webdriver 2.x

I am not able to click on the below HTML values through selenium webdriver click command through Java.
Here's my HTML...I have to click on PAAcctAcctRels, PAAcctActivityData, etc. as in the HTML.
I tried with LinkText (driver.findElement(By.linkText("PAAcctAcctRels")).click();) and xpath (driver.findElement(By.xpath(".//[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a")).click();)
<div id="primaryNavLevel2Z6_0G5A11K0KGF200AIUB98T20G52" class="dropdown_1columns">
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20G53">
<strong>
<span lang="en" dir="ltr">
PAAcctAcctRels
<span class="wpthemeAccess"> currently selected</span>
</span>
</strong>
</a>
</div>
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GD4">
<span lang="en" dir="ltr">PAAcctActivityData</span>
</a>
</div>
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GT1">
<span lang="en" dir="ltr">PAAcctAddrEmail</span>
</a>
</div>
Is there any other way to do this..please let me know.
1- For Clicking on text 'PAAcctActivityData', you can use the below code:
driver.findElement(By.xpath("//span[.='PAAcctActivityData']")).click();
2- For Clicking on text 'PAAcctAddrEmail', you can use the below code:
driver.findElement(By.xpath("//span[.='PAAcctAddrEmail']")).click();
NOTE:- The above xpaths will locate thespan elements with exact innerHTML/text as 'PAAcctActivityData' or 'PAAcctAddrEmail', respectively.
By.linkText("PAAcctAcctRels") won't work because that link has more text (ie ' currently selected'), and the problem with your xpath is that is starts with .//
The following should work (I have avoided using * for performance)
By.xpath("//div[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a")
Try using //[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a/span
as xpath. Remove the initial '.' and add '/span' at the end.