rgarg121
rgarg121

Reputation: 3

Selecting elements using xpath

So very new here to Selenium but I'm having trouble selecting the element I want from this website. In this case, I got the x_path using Chrome's 'copy XPath tool.' Basically, I'm looking to extract the CID text (in this case 4004) from the website, but my code seems to be unable to do this. Any help would be appreciated!

I have also tried using the CSS selector method as well but it returns the same error.

chrome_options = Options()  
chrome_options.add_argument("--headless")  
chrome_options.binary_location = '/Applications/Google Chrome   Canary.app/Contents/MacOS/Google Chrome Canary'

driver= webdriver.Chrome()

chem_name = "D008294"
url = "https://pubchem.ncbi.nlm.nih.gov/#query=" + chem_name
driver.get(url)  


elements = driver.find_elements_by_xpath('//*[@id="collection-results-container"]/div/div/div[2]/ul/li/div/div/div/div[2]/div[2]/div[2]/span/a/span/span')

driver.close()

print(elements.text)

As of now, this is the error I receive: 'list' object has no attribute 'text'

Upvotes: 0

Views: 209

Answers (4)

Auto-Answer
Auto-Answer

Reputation: 75

enter image description here

xpath: //a[contains(@href, 'compound')]/span[@class='breakword']/span

you can use the "href" as your attribute reference since I noticed that it has unique value for each component.

Example: href="https://pubchem.ncbi.nlm.nih.gov/substance/53790330" href="https://pubchem.ncbi.nlm.nih.gov/compound/4004"

Upvotes: 0

supputuri
supputuri

Reputation: 14145

Here is the xpath that you can use.

//span[.='Compound CID']//following-sibling::a/descendant::span[2]

enter image description here

Why your script did not worked: I 2 issues in your code.

elements = driver.find_elements_by_xpath('//*[@id="collection-results-container"]/div/div/div[2]/ul/li/div/div/div/div[2]/div[2]/div[2]/span/a/span/span')

driver.close() # <== don't close the browser until you are done with all your steps on the browser or elements

print(elements.text) # <== you can not get text from list (python will through error here

How to fix it:

CID =  driver.find_element_by_xpath("//span[.='Compound CID']//following-sibling::a/descendant::span[2]").text # <== returning the text using find_element (not find_elements)

driver.close()

print(CID) # <== now you can print `CID` though browser closed as the value already stored in variable.

Upvotes: 2

murali selenium
murali selenium

Reputation: 3927

Using xpath provided chrome is always does not work as expected. First you have to know how to write xpath and verify it chrome console.

see these links, which helps you to know about xpaths

https://www.guru99.com/xpath-selenium.html

https://www.w3schools.com/xml/xpath_syntax.asp

In this case, first find the span contains text Compound CID and move to parent span the down to child a/span/span. something like //span[contains(text(),'Compound CID']/parent::span/a/span/span.

And also you need to findelement which return single element and get text from it. If you use findelements then it will return list of elements, so you need to loop and get text from those elements.

Upvotes: 0

Hieu Nguyen
Hieu Nguyen

Reputation: 862

Function driver.find_elements_by_xpath return list of Element. You should loop to get text of each element,

Like this:

for ele in print(elements.text):
    print(ele.text)

Or if you want to match first Element, use driver.find_element_by_xpath function instead.

Upvotes: 0

Related Questions