Home > Software design >  Find element by Xpath. How to split the element I don't want inside the Xpath
Find element by Xpath. How to split the element I don't want inside the Xpath

Time:02-15

I try to scrape a website using Selenium. I got a problem when I try to get the coins name. because there're 2 elements inside 'td' How can I get rid of another element I don't want. or keep track to only its first element. (I found this enter image description here

I wanted input to look like this so I can make dataframe out of it

['Civic(CVC)', 'Bitcoin SV(BSV)', 'Ethereum(ETH)', 'Bitkub Coin(KUB)', 'Compound(COMP)', 'Curve DAO Token(CRV)', .... ]

CodePudding user response:

If all the mini versions of the coin names have 3 letters you can do this

for name in coin_name:
    if len(name) == 3:
        coin_name.remove(name)

CodePudding user response:

As per your current output:

['Civic(CVC)', '(CVC)', 'Bitcoin SV(BSV)', '(BSV)', 'Ethereum(ETH)', '(ETH)', 'Bitkub Coin(KUB)', '(KUB)', 'Compound(COMP)', '(COMP)', 'Curve DAO Token(CRV)', '(CRV)', .... ]

You can skip every alternative element and create a new list using list comprehension as follows:

# coin_name = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tbody//tr//td[2]//span")))]
coin_name = ['Civic(CVC)', '(CVC)', 'Bitcoin SV(BSV)', '(BSV)', 'Ethereum(ETH)', '(ETH)', 'Bitkub Coin(KUB)', '(KUB)', 'Compound(COMP)', '(COMP)', 'Curve DAO Token(CRV)', '(CRV)']
res = [coin_name[i] for i in range(len(coin_name)) if i % 2 == 0]
print (res)

Console Output:

['Civic(CVC)', 'Bitcoin SV(BSV)', 'Ethereum(ETH)', 'Bitkub Coin(KUB)', 'Compound(COMP)', 'Curve DAO Token(CRV)']
  • Related