Reputation: 435
there are some elements with different xpath like 'code' in the website:
.td[1]/div[4]/div
.td[1]/div[3]/div
null
if the xpath = .td[1]/div[4]/div, then there should be another ISIN code in .td[1]/div[3]/div I want to access them all with:
driver.get('https://www.chinabondconnect.com/en/Primary/Primary-Information/Onshore.html')
wait = WebDriverWait(driver, 30)
driver.find_element_by_link_text('Others').click()
try:
codes=[code.get_attribute('textContent') for code in driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[1]/div[4]/div")]
ISINs=[ISIN.get_attribute('textContent') for ISIN in driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[1]/div[3]/div")]
except:
try:
codes=[code.get_attribute('textContent') for code in driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[1]/div[3]/div")]
except:
codes = 'null'
#dateframe=...
But it will not return all issuers in the website, only just few ones, not sure why this happened, any help will be appreciated!
Upvotes: 0
Views: 137
Reputation: 9969
So far I have something like this. Which inserts null for no child elements of that td.
driver.get('https://www.chinabondconnect.com/en/Primary/Primary-Information/Onshore.html')
driver.find_element_by_link_text('Others').click()
codes=[]
ISINs=[]
rows=driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]")
for row in rows:
try:
codes.append(row.find_element_by_xpath("./td[1]/div[4]/div").get_attribute('textContent'))
except:
codes.append('null')
try:
ISINs.append(row.find_element_by_xpath("./td[1]/div[3]/div").get_attribute('textContent'))
except:
ISINs.append('null')
dataframe=pd.DataFrame({'codes':codes,'ISINs':ISINs})
print (dataframe)
Import
import pandas as pd
Upvotes: 1