I want to select contact information by selenium on the website below:
http://buyersguide.recyclingtoday.com/search.
For matching the right information one by one, I want to select the rows first, and then select information from the rows. The simple code as below, my question now is how to select the information from each row. For example, company name, email.
Code:
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import pandas as pd
driver = webdriver.Chrome('D:\chromedriver_win32\chromedriver.exe')
driver.get('http://buyersguide.recyclingtoday.com/search')
rows = driver.find_elements_by_xpath('//*[@id="Body_tbl"]/tbody/tr')
for row in rows:
email = row.find_element_by_xpath('//*/tr/td[3]/a').text
company=row.find_element_by_xpath('//*/tr/td[1]').text
Run the code as answers below, but I still face problem?
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import pandas as pd
driver = webdriver.Chrome('D:\chromedriver_win32\chromedriver.exe')
driver.get('http://buyersguide.recyclingtoday.com/search')
rows = driver.find_elements_by_xpath('//*[@id="Body_tbl"]/tbody/tr')
records = []
for row in rows:
company=row.find_element_by_xpath('./td[1]').text
address = row.find_element_by_xpath('./td[2]').text
contact= row.find_element_by_xpath('./td[3]//a').text
number= row.find_element_by_xpath('./td[5]').text
records.append((company,address,contact,number))
df = pd.DataFrame(records, columns=['company','number','address', 'contact'])
No content selected
You can get details like,
You have to locate number of Row available in the table without Table Header,
This is Example as according to your HTML.
Example using Python:
rows = driver.find_elements_by_xpath("//td[@style='font-weight:bold;']//parent::tr")
for row in rows:
company=row.find_element_by_xpath('./td[1]').text
address = row.find_element_by_xpath('./td[2]').text
contact= row.find_element_by_xpath('./td[3]//a').text
number= row.find_element_by_xpath('./td[5]').text
Example using Java:
List<WebElement> findData = driver.findElements("//td[@style='font-weight:bold;']//parent::tr");
for (WebElement webElement : findData) {
String getValueofCompany = webElement.findElement(By.xpath("./td[1]")).getText();
String getValueofAddress = webElement.findElement(By.xpath("./td[2]")).getText();
String getValueofContact = webElement.findElement(By.xpath("./td[3]//a")).getText();
String getValueofPhoneNumber = webElement.findElement(By.xpath("./td[5]")).getText();
}