Dynamic search unable to show the result through selenium webdriver? - selenium

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

Related

Unable to select checkboxes with a list item in a dropdown list due to Timeoutexception

I am trying to perform automation on a web page using chrome web driver in selenium python.
In one case i have a drop down list with 80 list items ,each list item preceeding with an input check box. I need to click on 2 check boxes from a given list matching with list item text.
Unable to select check boxes with a list item in a drop down list due to TimeoutException.
I am getting Timeout exception while trying to click on list item with check boxes in a drop down.
Note:
1.Also observed a loading indicator appears once a drop down list is clicked using moveToElement.
2.Dropdown list has around 80 list items and it has scroll bar.
3.This dropdown list is present immediate to another drop down listed in below link.
I followed :Not able to select a list item in an unordered list using selenium python
Started using webdriver wait with expected Conditions concept.
HTML :
HTML code:
=========
<div class="ms-drop bottom" style="display: block;">
<ul style="max-height: 400px;">
<li class="ms-select-all">
<label><input type="checkbox" data-name="selectAlls_osVer">
[Select all]
</label>
</li>
<li class="" style="false">
<label class=""><input type="checkbox" data-name="selectItems_osVer" value="KK">
<span>
apq40
</span>
</label>
</li>
<li class="" style="false">
<label class=""><input type="checkbox" data-name="selectItems_osVer" value="KK_MR1">
<span>
sdm845
</span>
</label>
</li>
<li class="" style="false">
<label class=""><input type="checkbox" data-name="selectItems_osVer" value="KK_MR2">
<span>
msm8996
</span>
</label>
</li>
<li class="" style="false">
<label class=""><input type="checkbox" data-name="selectItems_osVer" value="KK_MR2">
<span>
msm8996e
</span>
</label>
</li>
.....
..... so on (approximately 80 elements are there)
</ul>
</div>
****Tried with below code but getting Time out exception:****
Language:Python
Driver:Selenium
Browser:chrome
webdriver:Chromewebdriver
I am getting Timeout exception while trying to click on listitem with check boxes in a drop down list .
chipset = [ 'sdm845', 'msm8996e' ]
for i in range(len(chipset)):
chipset_name = "{}".format(chipset[i])
WebDriverWait(driver,20).until(EC.element_to_be_clickable
((By.XPATH,"//div[#class='ms-drop bottom']//ul//li[.//span
[normalize-space(text())='"+ chipset_name + "']]//input"))).click()
Note:
1.Also observed a loading indicator appears once a drop down list is clicked using moveToElement.
2.Drop down list has around 80 list items and it has scroll bar.
This drop down list is present immediately to another drop down.
My element is visible and it is 2nd & 4th list items to be selected in a drop down and it is visible. Though Time Out Exception occurs.
Is it due to loading icon appears ?
Also i would like to know how to go ahead if my desired list item to be selected is invisible and it appears at the end of the list and to make it visible i need to perform scroll down the list.
Expected:Need to perform click operation on desired list item input check boxes matching with the each string in a given list.
Actual: Time out Exception Appears while trying to find a list item in a dropdown containing a scroll bar.
Based on your code you wait till 20 second , for the elements in drop down to be loaded, but maybe you have issues with scroll in Selenium that cannot find all elements (all options), the trick here is to wait for only the first option in combo box, then you should know that drop down is opened and you can choose element,

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.

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

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

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.