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());
Related
I have a scenario where 15 questions are there and i do have to select the answer either by selecting radiobutton or checkbox or Yes/No button or have to enter some text in textarea.
My following code successfully selecting the answer randomly whether its a radio/checkbox or Yes/No button and displays the selected option as an answer along with question on console.
But how do i check if its textarea and need to enter something with 'sendKeys? And how do i select more than one checkboxes? And how do i display the answer Yes or No onconsole?
public void AssessmentTest() throws Exception
{
List<WebElement>totalQSN = driver.findElements(By.xpath("//div[#id='assessmentQuestionAnswersContainer']/div"));
List<WebElement> mainQuestions = driver.findElements(By.xpath("//div[#id='assessmentQuestionAnswersContainer']/div/div[2]/div"));
System.out.println("The Questions are::");
for(int i=0; i<totalQSN.size()-5; i++)
{
System.out.println("QUESTION:- " + mainQuestions.get(i).getText() + "["+ i +"]" );
Random rnd = new Random();
List<WebElement> subOptions = totalQSN.get(i).findElements(By.tagName("input"));
WebElement sValue = subOptions.get(rnd.nextInt(subOptions.size()));
sValue.click();
List<WebElement> subQsnList = driver.findElements(By.xpath("//div[#id='assessmentQuestionAnswersContainer']/div/div[3]/div/span/span"));
System.out.println(subOptions.size());
for(int j=0; j<subOptions.size(); j++)
{
if(subOptions.get(j).isSelected())
{
String selectedAnswer= subQsnList.get(j).getText();
System.out.println("ANSWER: - " + selectedAnswer);
System.out.println("\n");
}
}
} }
HTML Code is as under for each type of question.
<div id="individualQuestionAnswerContainer147" class="individual-question-answer-container" style="display: block;" xpath="1">
<div class="individual-questions-count-container"></div>
<div id="questionContainer147" style="clear: both; float: left;"></div> //Contains Question
<div id="questionAnswerContainer147" class="answers-container"></div> //Contains Answers
</div>
If the question having checkboxes, the code is
<div id="individualQuestionAnswerContainer148" class="individual-question-answer-container" style="" xpath="2"> Contains 4 checkbox(divs)
<div class="individual-questions-count-container"></div>
<div id="questionContainer148" style="clear: both; float: left;"></div>
<div id="questionAnswerContainer148" class="answers-container" style="">
<div class="answer-text">
<span class="answer-text-inner btn-assessment-answer">
<input type="checkbox" name="Checkbox148" id="Checkbox449" class="radio-assessment-answer">
<span style="display: table;">
Good
</span>
</span>
</div>
<div class="answer-text">
<span class="answer-text-inner btn-assessment-answer">
<input type="checkbox" name="Checkbox148" id="Checkbox450" class="radio-assessment-answer">
<span style="display: table;">
Mold
</span>
</span>
</div>
<div class="answer-text"></div>
<div class="answer-text"></div>
</div>
</div>
If question having 2 buttons(Yes/No), the code is
<div id="individualQuestionAnswerContainer155" class="individual-question-answer-container" style="" xpath="7"> Contains 2 options Yes or No
<div class="individual-questions-count-container"></div>
<div id="questionContainer155" style="clear: both; float: left;"></div>
<div id="questionAnswerContainer155" class="answers-container" style="">
<input type="button" value="Yes" id="Button477" class="btnClass_155 btn-assessment-answer" style="">
<input type="button" value="No" id="Button478" class="btnClass_155 buttonClicked btn-assessment-answer">
</div>
</div>
If question having textarea, the code is
<div id="questionAnswerContainer38" class="answers-container" xpath="1">
<textarea maxlength="50000" rows="2" cols="100" id="FreeTextarea38"></textarea>
</div>
Try using this structure..Hope this helps.
public void AssessmentTest() throws Exception
{
List<WebElement>totalQSN = driver.findElements(By.xpath("//div[#id='assessmentQuestionAnswersContainer']/div"));//Assuming this xpath is correct
List<WebElement> mainQuestions = driver.findElements(By.xpath("//div[#id='assessmentQuestionAnswersContainer']/div/div[2]/div")); //Assuming this xpath is correct
System.out.println("The Questions are::");
for(int i=0; i<totalQSN.size()-5; i++)
{
System.out.println("QUESTION:- " + mainQuestions.get(i).getText() + "["+ i +"]" );
Random rnd = new Random();
String webeleTag=totalQSN.get(i).getTagName();
if(webeleTag.equals("input"))){
String webeleType=totalQSN.get(i).getAttribute("type");
switch(webeleType){
case:"checkbox":
//your code to select check box
case:"button":
//Only click event on answer
default:
}
}
else if(webeleTag.equals("textarea")){
//your code
}
else{
//Not text area,checkbox or radio something else
}
}
}
To detect if it is a text area, from the top of my head you can go
for .getTagName() this will return the tag of the element.
To get all the checkboxes you can use driver.findElements(By.id("questionAnswerContainer148")).toArray(new WebElement[0]) , this will return an array with the checkboxes and you can use some logic to click in the items you want from the list.
For the yes/no, I think that the driver.findElement(by).getAttribute("value") will return the content of the attribute.
Hope this helps you.
Am trying to loop through a list and display them in a grid with four cards.
<div class="grid-x movies">
<div v-for="item in filteredMovie" :key="item.id">
<div class="cell large-4 medium-3 small-12">
<div class="card">
<img v-bind:src="item.cover_image" style="height:100px;width: 100px">
{{ item.name }}<br/>
</div>
</div>
my computed property is
computed:{
items(){
return this.$store.getters.getMovies
},
filteredMovie:function(){
let self= this;
return this.items.filter(function(item){
return item.name.toLowerCase().indexOf(self.search.toLowerCase()) >= 0
|| item.cast.toLowerCase().indexOf(self.search.toLowerCase()) >= 0
|| item.genre.toLowerCase().indexOf(self.search.toLowerCase()) >= 0;
}
)
}
}
}
</div>
but the list doesn't display on the page but when i put the v-for before the grid the list shows but the cards are not in four columns.what could be the problem
You need to put your classes on the looping div:
<div class="cell large-4 medium-3 small-12" v-for="item in filteredMovie" :key="item.id">
Your code in a sample: https://jsfiddle.net/5b6zkLom/
I have a div element in TouchXML, and I know for a fact that it contains an a element which I need to access. This is the element, which is declared as CXMLElement *element in a for-in loop
<div id="song_html" class="show3">
<div class="left">
<!-- info mp3 here -->
128 kbps<br/>2:35<br/>2.36 mb
</div>
<div id="right_song">
<div style="font-size:15px;"><b>The Doors - The Crystal Ship mp3</b></div>
<div style="clear:both;"/>
<div style="float:left;">
<div style="float:left; height:27px; font-size:13px; padding-top:2px;">
<div style="float:left;">Download</div>
<div style="margin-left:8px; float:left; width:27px; text-align:center;">Play</div>
<!-- New ad start -->
<script><![CDATA[
var id_array = ["newad14851327"]; window.onload = function(){
var i = 0;
for(i=0; i < id_array.length; i++){
var tgt = document.getElementById(id_array[i]).href; document.getElementById(id_array[i]).href = ""; document.getElementById(id_array[i]).onclick = function(){
window.location.href= tgt;
return false;
};
} }
]]>
</script><!-- New ad end -->
<div style="margin-left:8px; float:left;">Download Album</div>
<!-- ToneFuse Start -->
<script><![CDATA[ (function(id) {
console.log("tonefuse id:"+id);
document.write('<div style="margin-left:8px; float:left; color:red;" id="'+id+'">]]>
</script>
</div>
');
tfp_multi_ad_slots = window.tfp_multi_ad_slots || [];
tfp_multi_ad_slots.push(id);
window.tfpWriteMultiAd && tfpWriteMultiAd();
})(("tfp_multi_"+Math.random()).replace(".",""));
<!-- ToneFuse End -->
<div style="clear:both;"/>
</div>
<div id="player14851327" style="float:left; margin-left:10px;" class="player"/>
</div>
<div style="clear:both;"/>
</div>
(Sorry about the formatting; I didn't write it)
As you can see, there is an a element (there's actually two) in this. However, when I attempt to access these elements using the following
NSArray *linkElements = [element elementsForName:#"a"];
linkElements always contains 0 objects. Why is TouchXML not filling linkElements with the two a elements?
The elementsForName: method only looks at the immediate child nodes of the element. It does not do a recursive descent. In your posted code, element does not have any a elements, just more div child elements. You need to write your own code to recursively traverse all of the child div elements until you find all of the desired a elements.
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);
}
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!