Selenium webdriver autocomplete - selenium

I have input like
<input id="idForm:j_idt21Input" class="rf-sel-inp" type="text" value="Начните ввод" name="idForm:j_idt21Input" autocomplete="off">
and divs with variants
<div id="idForm:j_idt21Items">
<div id="idForm:j_idt21Item0" class="rf-sel-opt">Все</div>
<div id="idForm:j_idt21Item1" class="rf-sel-opt">Domosti Main (Domosti Main)</div>
<div id="idForm:j_idt21Item2" class="rf-sel-opt rf-sel-sel">N&K (N&K)</div>
<div id="idForm:j_idt21Item3" class="rf-sel-opt">АГМ (АГМ)</div>
<div id="idForm:j_idt21Item4" class="rf-sel-opt">АЕС Групп (АЕС Групп)</div>
<div id="idForm:j_idt21Item5" class="rf-sel-opt">АРКО (АРКО)</div>
<div id="idForm:j_idt21Item6" class="rf-sel-opt">АТМ-комплект (АТМ-комплект)</div>
<div id="idForm:j_idt21Item7" class="rf-sel-opt">Авита (Авита)</div>
<div id="idForm:j_idt21Item8" class="rf-sel-opt">Аква С. (Аква С.)</div>
<div id="idForm:j_idt21Item9" class="rf-sel-opt">Акваарт-М (Акваарт-М)</div>
<div id="idForm:j_idt21Item10" class="rf-sel-opt">Актив групп (Актив групп)</div>
<div id="idForm:j_idt21Item11" class="rf-sel-opt">Алан-Трейд (Алан-Трейд)</div>
<div id="idForm:j_idt21Item12" class="rf-sel-opt">Алекон (Алекон)</div>
<div id="idForm:j_idt21Item13" class="rf-sel-opt">Алекс трейд (Алекс трейд)</div>
<div id="idForm:j_idt21Item14" class="rf-sel-opt">Альт-М (Альт-М)</div>
<div id="idForm:j_idt21Item15" class="rf-sel-opt">Альфа (Альфа)</div>
<div id="idForm:j_idt21Item16" class="rf-sel-opt">Ансан (Ансан)</div>
<div id="idForm:j_idt21Item17" class="rf-sel-opt">Ария Текстиль (Ария Текстиль)</div>
<div id="idForm:j_idt21Item18" class="rf-sel-opt">Арреал 2000 (Арреал 2000)</div>
</div>
I tried
webDriver.findElement(By.xpath("//td[text()='Поставщики']/following-sibling::td//input[contains(#id, 'Input')]")).sendKeys("Поливалент");
webDriver.findElement(By.xpath("//td[text()='Поставщики']/following-sibling::td//input[contains(#id, 'Input')]")).sendKeys(Keys.ARROW_DOWN);
webDriver.findElement(By.xpath("//td[text()='Поставщики']/following-sibling::td//input[contains(#id, 'Input')]")).sendKeys(Keys.ENTER);
but it's not working
Help, how to select variant from this input with selenium webdriver?

try this variant(using css selector):
webDriver.findElement(By.cssSelector("input.rf-sel-inp")).sendKeys("Поливалент");
or another way using jscript :
String cssSelector = .... //css selector of the element u need to select
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
Hope this works for you

Try this-
driver.findElement(By.xpath("//input[#value='Начнитеввод']")).sendKeys("Поливалент");
Let me know if its not working!

Related

Not able to identify check box based on the name given beside it using xpath

I wanted to identify the CheckBox based on the name given to it by using the xpath, but was not able to reach till the text uniquely.
The html code is in below. How can I get the dynamic xpath for 'text1' or 'text2' mentioned in the html?
<html>
<body>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text1
</div>
</div>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text2
</div>
</div>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text3
</div>
</div>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text4
</div>
</div>
</body>
</html>
The below code worked for me
driver.findElement(By.xpath("//div[contains(.,'text4')]/input")).click();
To identify the node with text as text1 you can use the following line of code :
WebElement element_text1 = driver.findElement(By.xpath("//div[#class='section-content']/div[contains(normalize-space(), 'text1')]"));
To get identify the Check Box associated with the node with text as text1 you can use the following line of code :
driver.findElement(By.xpath("//div[#class='section-content']/div[contains(normalize-space(), 'text1')]/input"));
Try this:
WebElement body = dr.findElement(By.xpath("/html/body"));
List<WebElement> div = dr.findElements(By.className("section-content"));
for(int i=1;i<div.size();i++)
{
String checkBoxText = dr.findElement(By.xpath("/html/body/div["+i+"]/div")).getText();
if(checkBoxText.equals("text3"))
{
dr.findElement(By.xpath("/html/body/div["+i+"]/div/input")).click();
break;
}
}

m is not defined materialize css

I always get error "M is not defined" el is not defined
codepen sample
if you open console, you will see the error
I first load jquery.min.js then load materialize.min.js .css
then load my script.js below.
M is not recognized, which it should
why?
// init materialize tab
var instance = M.Tabs.init(el, options);
// Or with jQuery
$(document).ready(function(){
$('.tabs').tabs();
});
This is html
<div class="row">
<div class="col s12">
<ul class="tabs">
<li class="tab col s3">Test 1</li>
<li class="tab col s3"><a class="active" href="#test2">Test 2</a></li>
<li class="tab col s3 disabled">Disabled Tab</li>
<li class="tab col s3">Test 4</li>
</ul>
</div>
<div id="test1" class="col s12">Test 1</div>
<div id="test2" class="col s12">Test 2</div>
<div id="test3" class="col s12">Test 3</div>
<div id="test4" class="col s12">Test 4</div>
</div>
I found the problem, it is version.
Make sure you use version => 1.0.0.
https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js
If you use version <1.0.0 very likely you have error as 'M is not defined'
Make sure you load jquery before materialize css !
Also you must first define elem and options to init tab
// init materialize tab
var elem = $('.tabs')
var options = {}
var instance = M.Tabs.init(elem, options);
//or Without Jquery
//var elem = document.querySelector('.tabs');
var options = {}
var instance = M.Tabs.init(elem, options);

Count some images Using Selenium

I have the code below and want to count only the images in "galeria003" (I have others galleries with imgs on the page)
<div id="classepai" class="carrossel">
<div class="galeria003">
<div class="conjuntoimgs">
<div class="imagem_item">
<div>
<img src="https://localhost/foto1.jpg">
</div>
</div>
<div class="imagem_item">
<div>
<img src="https://localhost/foto2.jpg">
</div>
</div>
</div>
</div>
<div class="galeria004">
<div class="conjuntoimgs">
(...)
</div>
</div>
I tried with the code below, but it returns to me all the images on the page.
public int galeria03() {
List<WebElement> lista03 = driver.findElements(By.tagName("img"));
int count = 0;
for(WebElement e : lista03) {
count++;
}
return count;
You can use below xpath to get all image tag present in "galeria003"
List<WebElement> lista03 = driver.findElements(By.xpath("//div[#class='galeria003']//img"));
And print the total image element
System.out.println(lista03.size());

find values in code using selenium

I need to print names of friends list of facebook account which are in the code.I am using java, webdriver, eclipse. so how can i do it..?
My code is:
hc_location=friends_tab">
<div class="clearfix _42ef">
<div class="_6a rfloat _ohf">
<div class="uiProfileBlockContent">
<div class="_6a">
<div class="_6a _6b" style="height:100px"/>
<div class="_6a _6b">
<div class="fsl fwb fcb">
<a data-hovercard="/ajax/hovercard/user.php?id=100004354923588&extragetparams=%7B%22hc_location%22%3A%22friends_tab%22%7D" data-gt="{"engagement":{"eng_type":"1","eng_src":"2","eng_tid":"100004354923588","eng_data":[]},"coeff2_registry_key":"0406","coeff2_info":"AatFZB3bSKV1-v1-TR2ok-dPAbN9rzl_3kU0pGsa25fiWaVHx5-bjHLWKDd3viMwgv1yaRLvlMdX3-X03tbhtjEZ","coeff2_action":"1","coeff2_pv_signature":"738277089"}" href="https://www.facebook.com/sivaramakrishna.churukuri?fref=pb&hc_location=friends_tab">Sivaramakrishna Churukuri</a>
</div>
<a class="uiLinkSubtle" data-gt="{"coeff2_registry_key":"0406","coeff2_info":"AauP9VE6r6RJg9RklXss8Ij7rBBpi8gQXqOJbBK3dhvJV9-qk6TEr1oJklIPahLAfMkkWVB_SIlPbQ6vlwDJIe13","coeff2_action":"13","coeff2_pv_signature":"738277089"}" href="https://www.facebook.com/sivaramakrishna.churukuri/friends">103 friends</a>
</div>
</div>
</div>
</div>
</div>
</li>
<li class="_698">
<div class="clearfix _5qo4">
<a class="_5q6s _8o _8t lfloat _ohe" data-hovercard="/ajax/hovercard/user.php?id=100003212947042&extragetparams=%7B%22hc_location%22%3A%22friends_tab%22%7D" aria-hidden="true" tabindex="-1" href="https://www.facebook.com/kamesh.peri.5?fref=pb&hc_location=friends_tab">
<div class="clearfix _42ef">
<div class="_6a rfloat _ohf">
<div class="uiProfileBlockContent">
<div class="_6a">
<div class="_6a _6b" style="height:100px"/>
<div class="_6a _6b">
<div class="fsl fwb fcb">
<a data-hovercard="/ajax/hovercard/user.php?id=100003212947042&extragetparams=%7B%22hc_location%22%3A%22friends_tab%22%7D" data-gt="{"engagement":{"eng_type":"1","eng_src":"2","eng_tid":"100003212947042","eng_data":[]},"coeff2_registry_key":"0406","coeff2_info":"AauBaMRFF-E1ITEW9Rva9NO6xU67IbSuJZEgYIzHEB4CVZ_e6MM2fHCqF75opZvYnSlnHSOqYQ3EaZucFsMq6WMd","coeff2_action":"1","coeff2_pv_signature":"738277089"}" href="https://www.facebook.com/kamesh.peri.5?fref=pb&hc_location=friends_tab">Kamesh Peri</a>
</div>
<a class="uiLinkSubtle" data-gt="{"coeff2_registry_key":"0406","coeff2_info":"Aat-c_R0rmMkazYv1tQfMWB254d055vp_28IHeIbPNodi5AgjkwSKK0gxoikjPCHdstPnIZgBGM4DLQexsa3ctZ5","coeff2_action":"13","coeff2_pv_signature":"738277089"}" href="https://www.facebook.com/kamesh.peri.5/friends">374 friends</a>
</div>
</div>
</div>
</div>
</div>
</li>
<li class="_698">
<div class="clearfix _5qo4">
<a class="_5q6s _8o _8t lfloat _ohe" data-hovercard="/ajax/hovercard/user.php?id=678773097&extragetparams=%7B%22hc_location%22%3A%22friends_tab%22%7D" aria-hidden="true" tabindex="-1" href="https://www.facebook.com/rchalasani?fref=pb&hc_location=friends_tab">
<div class="clearfix _42ef">
<div class="_6a rfloat _ohf">
<div class="uiProfileBlockContent">
<div class="_6a">
<div class="_6a _6b" style="height:100px"/>
<div class="_6a _6b">
<div class="fsl fwb fcb">
<a data-hovercard="/ajax/hovercard/user.php?id=678773097&extragetparams=%7B%22hc_location%22%3A%22friends_tab%22%7D" data-gt="{"engagement":{"eng_type":"1","eng_src":"2","eng_tid":"678773097","eng_data":[]},"coeff2_registry_key":"0406","coeff2_info":"AasX8OsfTavfyAhEpE-iOv9PuaD2vgAhBs9ByrQ72VN1TWGanfz8Cc6UlLt7hsMf-Js","coeff2_action":"1","coeff2_pv_signature":"738277089"}" href="https://www.facebook.com/rchalasani?fref=pb&hc_location=friends_tab">Rama Chalasani</a>
</div>
<a class="uiLinkSubtle" role="button" rel="dialog" href="/browse/mutual_friends/?uid=678773097" ajaxify="/ajax/browser/dialog/mutual_friends/?uid=678773097" data-tooltip-uri="/ajax/mutual_friends/tooltip.php?friend_id=678773097" data-hover="tooltip">24 mutual friends</a>
I need to print Sivaramakrishna Churukuri, Kamesh Peri, Rama Chalasani which are in
<a></a>
tags
Thanks in advance.
I have tried this
driver.findElement(By.id("email")).sendKeys("smallfishhh4#gmail.com");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("password");
// driver.findElement(By.id("u_0_n")).click();
driver.findElement(By.xpath("//label[#id='loginbutton']/input")).click();
driver.findElement(By.className("headerTinymanName")).click();
driver.findElement(By.xpath("//a[#data-medley-id='pagelet_timeline_medley_friends']")).click();
// List<WebElement> ele = driver.findElement(By.className("fsl fwb fcb"));
List<WebElement> allNames = driver.findElements(By.xpath("//div[#class='fsl fwb fcb']/a"));
//List<WebElement> allNames = driver.findElements(By.xpath("//div[#class='uiProfileBlockContent']/a"));
int num = driver.findElements(By.xpath("//ul")).size();
System.out.println(num);
System.out.println(allNames.size());
for(int j=0; j<num; j++){
for(int i=0;i<allNames.size();i++){
System.out.println(allNames.get(i).getText());
names = names+allNames.get(i).getText();
}
}
Get friend list using Selenium is web scraping and you should not do it, its illegal. Instead use the graph api with Java to achieve your goal. Below is one example. Another option you can use is RestFB(http://restfb.com/legacy-rest-api.html)
public void getIds(){
Session session = Session.getActiveSession();
String query = "select uid from user where uid in (select uid2 from friend where uid1 = me())";
Bundle params = new Bundle();
params.putString("q", query);
Request request = new Request(session, "/me/friends", params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
Log.d("Id's :", "Response = "+response);
}
});
Request.executeBatchAsync(request);
}

Issue with Drag and Drop element in Selenium Web driver with Java

In left pane there are some widgets the below code gives some idea about how the widgets are organized....
<div id="sites-widgets-1375699547529" class="sites-widget-container tab-content-div frog-touch-scroll active">
<div class="ui-editor-box">
<div class="image draggable" data-content="file/widget/0141D9112001BD9824CA7FB813F3CF04088C02AC50F154FA/icon.png" data-uuid="0141D9112001BD9824CA7FB813F3CF04088C02AC50F154FA" style="background-image:url(file/widget/0141D9112001BD9824CA7FB813F3CF04088C02AC50F154FA/icon.png);"/>
<div class="ui-editor-box-label">Text Activity</div>
</div>
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
<div class="ui-editor-box">
</div>
I want to drag and drop a widget to the container provided in the same web page. the code for the container is given below
<div class="sites-layout-one-one">
<div style="clear:both;"/>
<div class="bucket ui-sortable edit" data-order="1" data-attr="bucket"/>
<div style="clear:both;"/>
</div>
I'm trying to drag and drop the widget in to the container, but i cant able to do this, the code that i wrote is below
WebElement dragElement = driver.findElement(By.xpath("//div[#class='sites-widget-container tab-content-div frog-touch-scroll active']/div[#class='ui-editor-box'][1]"));
WebElement dropElement = driver.findElement(By.xpath("//div[#class='sites-layout-one-one']"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(dragElement)
.moveToElement(dropElement)
.release(dropElement)
.build();
dragAndDrop.perform();
Thread.sleep(10000);
Please help me to sort out this...
Thanks in Advance
Shiva..
Just use the built in dragAndDrop action to handle this for you:
Actions builder = new Actions(driver);
builder.dragAndDrop(dragElement, dropElement).perform();
The API documentation is located here, which shows the usage for the dragAndDrop action:
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html