0

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!

6
  • there are only 12 isin and 53 code, what did you expect? Commented Mar 3, 2021 at 7:56
  • Hi, but I expect the no value ones could return 'null' in dataframe, but it just eliminant the ones do not have code Commented Mar 3, 2021 at 8:16
  • so you want the isin and code length equal? use normal loop. Commented Mar 3, 2021 at 8:19
  • Did you want all 200 or so rows of it. Commented Mar 4, 2021 at 5:50
  • @ArundeepChohan Hello, yes, I'd like to get all recorded issuers' codes/ISIN info (if does not have, just return 'null' or blank into the csv dataframe) also for matrurity the xpath is different ("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[3]/div[3]/span[2]")or div[5], not sure how to deal with that situation.. Commented Mar 4, 2021 at 6:27

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thanks for your help! but if I also want issuer and dates in the dataframe, I also need to create empty list right? or just another for loop for issuers :issuers=[issuer.get_attribute('textContent') for issuer in driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[1]/div[2]/div")]
Yup if you want to append null you need something to append to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.