how to findall class using beautifulsoup when class has random offerid? im trying using below syntax but it don't get anything
containers = page_soup.findAll("div",{"class":xyz "})
class example is as below
<div class=abc">
<div class=bcd">
<div class="xyz " offerid="65546">
<div class="xyz " offerid="46465">
<div class="xyz " offerid="56747">
</div>
</div>
Use CSS Selector to get the items with class name.Try following example.
data = '''
<div class="xyz " offerid="65546">Test 1</div>
<div class="xyz " offerid="46465">Test 2</div>
<div class="xyz " offerid="56747">Test 3</div>
'''
soup = BeautifulSoup(data,'html.parser')
containers = soup.select("div.xyz")
for item in containers:
print(item.text)
OR
data = '''
<div class="xyz " offerid="65546">Test 1</div>
<div class="xyz " offerid="46465">Test 2</div>
<div class="xyz " offerid="56747">Test 3</div>
'''
soup = BeautifulSoup(data,'html.parser')
containers = soup.select(".xyz")
for item in containers:
print(item.text)
EDITED:
data = '''
<div class=abc">
<div class=bcd">
<div class="xyz " offerid="65546">
<div class="xyz " offerid="46465">
<div class="xyz " offerid="56747">
</div>
</div>
'''
soup = BeautifulSoup(data,'html.parser')
containers = soup.select("div.xyz[offerid]")
for item in containers:
print(item['offerid'])
I am not 100% clear on your quesiton but the ID shouldn't matter if you are picking up the div via the class name. The below sample code works.
html = '''
<div class="xyz " offerid="65546"> sample text </div>
<div class="xyz " offerid="46465"> sample text </div>
<div class="xyz " offerid="56747"> sample text </div>
'''
soup = BeautifulSoup(html,'html.parser')
containers = soup.find_all("div",{"class":"xyz "})
Perhaps you want an attribute selector
data = [item['offerid'] for item in soup.select('[offerid]')]
or
data = [item.text for item in soup.select('[offerid]')]
Related
Hello everyone I have the information I want pulled using BeautiuflSoup but I can't seem to get it printed out correctly to send to pandas and excel.
html_f ='''
<li class="list-group-item">
<div>
<div class="tyler-toggle-controller open">
<p class="text-primary">
07/01/2022 Date
<span class="caret"> </span>
</p>
</div>
<div class="tyler-toggle-container row-buff" style="display: block; overflow: hidden;">
<p class="col-sm-12 col-md-12">
<span class="text-muted">Comment</span><br>
[1] Comments
</p>
</div>
</div>
</li>'''
My code used to pull the data I want:
soup = BeautifulSoup(html_f,'html.parser')
for child in soup.findAll('li',class_='list-group-item')[0]:
print (child.text)
Here is the info it pulls But it prints it out weird with tons of spacing
07/01/2022 Date
Comment
[1] Comments
Ideally, I only need the top portion of (date and File Date) printed out but at the very least I need help getting it into a list format like:
07/01/2022 Date
Comment
[1] Comments
To get your information printed as expected in your question, you could use stripped_strings and iterate over its elements:
for e in soup.find_all('li',class_='list-group-item'):
for t in list(e.stripped_strings):
print(t)
Note: In new code use find_all() instead of old syntax findAll().
Example
html='''
<li class="list-group-item">
<div>
<div class="tyler-toggle-controller open">
<p class="text-primary">
07/01/2022 Date
<span class="caret">
</span>
</p>
</div>
<div class="tyler-toggle-container row-buff" style="display: block; overflow: hidden;">
<p class="col-sm-12 col-md-12">
<span class="text-muted">
Comment
</span>
<br/>
[1] Comments
</p>
</div>
</div>
</li>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
for e in soup.find_all('li',class_='list-group-item'):
for t in list(e.stripped_strings):
print(t)
Output
07/01/2022 Date
Comment
[1] Comments
Not sure cause you are talking about pandas, you also could pick each information, clean it up and append to a list of dicts:
data = []
for e in soup.find_all('li',class_='list-group-item'):
data.append({
'date': e.p.text.strip().replace(' Date',''),
'comment': e.select_one('.tyler-toggle-container br').next_sibling.strip()
})
pd.DataFrame(data)
or
data = [{
'date':soup.select_one('li.list-group-item .text-primary').text.strip().replace(' Date',''),
'comment':soup.select_one('li.list-group-item .tyler-toggle-container br').next_sibling.strip()
}]
Output
date
comment
07/01/2022
[1] Comments
So far so good, it's my trying
doc='''
<li class="list-group-item">
<div>
<div class="tyler-toggle-controller open">
<p class="text-primary">
07/01/2022 Date
<span class="caret">
</span>
</p>
</div>
<div class="tyler-toggle-container row-buff" style="display: block; overflow: hidden;">
<p class="col-sm-12 col-md-12">
<span class="text-muted">
Comment
</span>
<br/>
[1] Comments
</p>
</div>
</div>
</li>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(doc, 'html.parser')
text=[' '.join(child.get_text(strip=True).split(' ')).replace(' DateComment[1]',',') for child in soup.find_all('li',class_='list-group-item')]
print(text)
Output:
['07/01/2022, Comments']
Try this ways,must work
text=' '.join([' '.join(child.get_text(strip=True).split(' ')).replace(' DateComment[1]',',') for child in soup.find_all('li',class_='list-group-item')]).strip()
#Or
text= [' '.join(child.get_text(strip=True).split(' ')).replace(' DateComment[1]',',') for child in soup.find_all('li',class_='list-group-item')]
final_text= text[1]+ ',' +text[2]
final_text= text[1]+text[2].split()#if you want to make list
I have a page with list of products on it.
This is how HTML DOM looks like for one product item:
<div class="module card listing-search-card js-product-card " id="product-entry-123" data-product-id="123" data-toggle-status="open" data-out-of-stock="" data-toggle-isbundle="false" data-load-prices-async="false">
<div class="product-entry__wrapper">
<div class="card__header">
<div class="promotion">
<div class="product-entry__right promotion-card__body on-promotion--banner-offer">
</div>
<a href="/Products/p/123" tabindex="-1">
<picture>
<img class="card__image mobile-img lazyload" src="/medias/image-mobile">
<img class="card__image desktop-img lazyloaded" src="/medias/image-desktop">
</picture>
</a>
</div>
</div>
<div class="product-entry__body-actions-wrapper">
<div class="product-entry__body card__body">
<h3 class="card__title">
Schweppes
</h3>
<div class="product-entry__summary card__description-wrapper">
<div class="product-entry__summary__list">
<div class="card__detail-wrapper">
<div class="product-entry__summary__item card__description-product-detail">
33 x 24</div>
<div class="product-entry__summary__item card__description-product-code">
<span class="product-entry__code">
123</span>
</div>
</div>
<div class="container-type">
box</div>
</div>
</div>
</div>
<div class="cta-container">
<div class="card__amount-wrapper ">
<div class="card__amount">
61,83 € <span class="base-unit">HT/CHACUN</span>
<p class="sales-unit-price is-price">
<span>soit</span> 10,00 €
</span></span></p>
</div>
</div>
<div class="add-to-cart__footer add-to-cart__action">
<div class="success-overlay">Add to cart</div>
<div class="add-to-cart__action--active">
<div class="form-quantity__wrapper quantity-action quantity-action__wrapper"
data-form-quantity-id="123">
<div class="form-quantity ">
<button class="form-quantity__decrease quantity-action__decr icon-Minus disabled" type="button"
tabindex="-1" aria-label="decrement" data-form-quantity-decrement="">
</button>
<input id="product-123" class="form-quantity__input form-control quantity-action__value js-
quantity-input-typing" name="product-123" type="text" value="1" maxlength="4" data-price-
single="10.00" data-price-currency="€" data-parsley-range="[1,9999]" data-form-quantity-times="1"
data-parsley-multiplerange="1" data-parsley-type="integer" data-parsley-validation-threshold="1"
required="">
<button class="form-quantity__increase quantity-action__incr icon-Add-to-list" type="button"
tabindex="-1" aria-label="increment" data-form-quantity-increment="">
</button>
</div>
<span class="form-quantity__update" data-form-quantity-success=""></span>
</div>
<div class="add-to-cart__total">
<button class="button button--primary js-addToCart" role="button" title="Add
to cart" data-product-id-ref="123" data-modal-trigger="" data-modal-target="#add-to-cart-modal" data-
modal-before-trigger="addToCart" data-component-id="product list" tabindex="-1">
<div class="button__text">
<span class="button__text-add js-added-price">Add</span>
<span class="button__text-to-cart js-added-price">to cart</span>
</div>
<span class="button__text js-added-price mobile-only">Add</span>
</button>
</div>
</div>
</div>
<div class="add-to-template">
<button class="add-to-template--button button js-addToNewTemplate" type="button" data-modal-
trigger="" data-modal-target="#add-to-template-modal" data-modal-before-
trigger="openAddToTemplateModal" data-product-code="123">
<span>Add to list</span>
</button>
</div>
</div>
</div>
</div>
I am calling this function:
isSortedAlphabeticallyAscending($$('div.js-product-card'));
And the function implementation is:
isSortedAlphabeticallyAscending(list) {
for (let i = 0; i < (list.length - 1); i++) {
let outOfStockCurrent = list[i].getAttribute('data-out-of-stock');
let outOfStockNext = list[i + 1].getAttribute('data-out-of-stock');
let idCurrent = list[i].getAttribute('id');
let idNext = list[i + 1].getAttribute('id');
console.log("outOfStockCurrent " + outOfStockCurrent + " " + idCurrent);
console.log("outOfStockNext " + outOfStockNext + " " + idNext);
let productIdCurrent = idCurrent.split('-').pop();
let productIdNext = idNext.split('-').pop();
let currentText = list[i].$('a[href*="' + productIdCurrent + '"]').getText();
let nextText = list[i+1].$('a[href*="'+ productIdNext + '"]').getText();
console.log("currentText " + currentText);
console.log("nextText " + nextText);
if(outOfStockCurrent === "true" || outOfStockNext === "true") continue;
if (currentText > nextText) return false;
}
return true;
}
I ignore out of stock products since they are always at the bottom of the page.
But the list[i].$('a[href*="' + productIdCurrent + '"]').getText() is always returning empty text.
I would like it to get "Schweppes" text, i.e. product name.
Is there a way to chain somehow differently part with .$a[href ...] to get the text from the <a> tag inside the <div> element of the list of products using webdriverio 5?
Thanks!
The above selector list[i].$('a[href*="' + productIdCurrent + '"]').getText() targeted 2 elements.
What I needed to go one div further and find it there:
list[i].$('div.product-entry__body-actions-wrapper').$('a[href*="' + productIdCurrent + '"]').getText()
And voila, text appeared :)
Hope it will help someone with the similar issue :D
I want to print some data using selenium, here is HTML:
<div class="details-row section box-margin-LRmd is-disabled">
<div class="inline-flex-group-v2">
<div class="item gutter-sm box-fixed col-encounter-type">
<label class="field-label">Encounter type</label>
<div data-element="encounter-details-encounter-type">
Office Visit
<!----> </div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding">
<label class="field-label">Note type</label>
<div data-element="encounter-details-encounter-type">Case Review</div>
</div>
<div class="item gutter-sm box-fixed col-date">
<label class="field-label">Date</label>
<div data-element="encounter-details-date" class="clearfix">
05/01/2018
</div>
</div>
<div class="item gutter-sm box-fixed col-age visible-lg">
<label class="field-label">Age at encounter</label>
<div data-element="encounter-details-age">60 yrs</div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding visible-xl">
<label class="field-label">Seen by</label>
<div data-element="encounter-details-seen-by">Kristina Abernathy</div>
</div>
<div class="item gutter-sm box-fixed col-extra-padding visible-xl">
<label class="field-label">Facility</label>
<div data-element="encounter-details-facility">FOCUS Pittsburgh Free Health Center</div>
</div>
<div class="item gutter-sm visible-xl">
<label class="field-label">Status</label>
<div>
<span data-element="encounter-details-signed-by">Electronically signed by Kristina Abernathy at 05/01/2018 07:21 pm</span>
</div>
</div>
<!----> </div>
<div class="inline-flex-group-v2 hidden-xl box-padding-Tmd-v2 lower">
<div class="item gutter-lg box-fixed col-age hidden-lg">
<label class="field-label">Age at encounter</label>
<div data-element="encounter-details-age">60 yrs</div>
</div>
<div class="item gutter-lg box-fixed col-seen-by">
<label class="field-label">Seen by</label>
<div data-element="encounter-details-seen-by">Kristina Abernathy</div>
</div>
<div class="item gutter-lg box-fixed">
<label class="field-label">Facility</label>
<div data-element="encounter-details-facility">Health Center</div>
</div>
<div class="item gutter-sm">
<label class="field-label">Status</label>
<div>
<span data-element="encounter-details-signed-by">Electronically signed by Kristina Abernathy at 05/01/2018 07:21 pm</span>
</div>
</div>
<!----> </div>
</div>
I want to print encounter type, note type, date, age and seen by. So I use code :
System.out.println("Encounter details");
System.out.println("ENCOUNTER TYPE: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-encounter-type']")).getText());
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-encounter-type']")).getText());
System.out.println("DATE: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-date']")).getText());
System.out.println("AGE OF ENCOUNTER: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-age']")).getText());
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[#data-element='encounter-details-seen-by']")).getText());
And I got:
Encounter details
ENCOUNTER TYPE: Office Visit
NOTE TYPE: Office Visit
DATE: 05/01/2018
AGE OF ENCOUNTER: 60 yrs
SEEN BY:
So encounter type and note type have same attribute "data-element='encounter-details-encounter-type'", that's why I got same result for "encounter details" and "encounter type", but what I want to get is
ENCOUNTER TYPE: Office Visit
NOTE TYPE: Case Review
DATE: 05/01/2018
AGE OF ENCOUNTER: 60 yrs
SEEN BY: Kristina Abernathy
So how should I get correct note type and why can't I get "SEEN BY" content? Thanks!
From your html I can see that the Encounter Type and Note Type field is using the same element for reference. That's why you are getting same result for the different headings. While you are selecting the locators for identifying an element it is better to use unique values for that element otherwise these kind of issues will occur. Please check with the below code for Note Type,
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//div[label='Note type']//*[#data-element='encounter-details-encounter-type']")).getText());
I have checked the SEEN BY field, it is working fine.
But if you want to take the first SEEN BY record, then use the below code,
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[contains(#class,'extra-padding visible-xl')]//*[#data-element='encounter-details-seen-by']")).getText());
For the second SEEN BY,
System.out.println("SEEN BY: " + driver.findElement(By.xpath("//div[contains(#class,'fixed col-seen-by')]//*[#data-element='encounter-details-seen-by']")).getText());
As per your requirement, you can use cssSelector for Note Type.
Code you can try out:
System.out.println("NOTE TYPE: " + driver.findElement(By.cssSelector("div[class$='col-extra-padding'] div")).getText());
If you still wanna go with Xpath, then you can try this code
Xpath :
//label[text()='Note type']/following-sibling::div
Code :
System.out.println("NOTE TYPE: " + driver.findElement(By.xpath("//label[text()='Note type']/following-sibling::div")).getText());
There are two Seen By:
For first one you can write cssSelector as :
div[class*='col-extra-padding']>div[data-element$='-seen-by']
Code
System.out.println("First Seen By: " + driver.findElement(By.cssSelector("div[class*='col-extra-padding']>div[data-element$='-seen-by']")).getText());
Xpath would be:
//div[contains(#class,'col-extra-padding')]/child::div[#data-element='encounter-details-seen-by']
Code:
System.out.println("First Seen By: " + driver.findElement(By.xpath("//div[contains(#class,'col-extra-padding')]/child::div[#data-element='encounter-details-seen-by']")).getText());
For Second Seen By you can write cssSelector as :
cssSelector :
div[class*='box-padding-Tmd'] div[class$='col-seen-by'] div
Code
System.out.println("Second Seen By: " + driver.findElement(By.cssSelector("div[class*='box-padding-Tmd'] div[class$='col-seen-by'] div")).getText());
Xpath :
//div[contains(#class,'box-padding-Tmd')]/child::div[contains(#class,'col-seen-by')]/child::div
Code
System.out.println("Second Seen By: " + driver.findElement(By.xpath("//div[contains(#class,'box-padding-Tmd')]/child::div[contains(#class,'col-seen-by')]/child::div ")).getText());
To retrieve the Encounter Details you can create two Lists one for the Headings and the other for the Values and print them as follows :
List<WebElement> items = driver.findElements(By.xpath("//div[#class='inline-flex-group-v2']//div[contains(#class,'item') and contains(#class,'gutter-sm') and contains(#class,'box-fixed')]/label"));
List<WebElement> value = driver.findElements(By.xpath("//div[#class='inline-flex-group-v2']//div[contains(#class,'item') and contains(#class,'gutter-sm') and contains(#class,'box-fixed')]//div[starts-with(#data-element,'encounter-details-')]"));
for(int i=0; i<items.size(); i++)
System.out.println(items.get(i).getText() + " value is : " + value.get(i).getText());
Add parent node in locator to narrow down the search scope of encounter type and note type.
The reason why you get empty on SEEN BY, I guess it's not visible on page or there are more than one elements be found by your locator and the first element has no text content. If it's not visible, you can use getAttribute("innerText") to get text content of element.
System.out.println("Encounter details");
System.out.println("ENCOUNTER TYPE: " + driver
.findElement(By.cssSelector("div.col-encounter-type div[data-element='encounter-details-encounter-type']"))
.getText());
System.out.println("NOTE TYPE: " + driver
.findElement(By.cssSelector("div.col-extra-padding div[data-element='encounter-details-encounter-type']"))
.getText());
System.out.println("DATE: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-date']"))
.getText());
System.out.println("AGE OF ENCOUNTER: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-age']"))
.getText());
System.out.println("SEEN BY: " + driver
.findElement(By.cssSelector("div[data-element='encounter-details-seen-by']"))
.getAttribute("innerText"));
hello everyone I would like to know numer of elements like the following
<div id="datatable">
<form id="theForm" name="theForm" >......</form>
<div class="no_data_dd" id="no_data" >....
<div class= ....>.....
<div class= ....>
</div>
</div>
</div>
<div class="score_row score_header">.../div>
<div class="score_row match_line e_true" >..</div>
<div class="score_row padded_date ">..</div>
<div class= ....>.....</div>
</div>
I tried with
Set itemEle = objIE.document.getElementById("scoretable")
Length = itemEle.getElementsByTagName("class").Length
length = 0 and nont =5
why?
What if you try get "elements" by instead of get "element" that might be a reason for the answer
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);
}