Home > database >  How to scrape data in selenium using selector
How to scrape data in selenium using selector

Time:10-28

I want to scrape/get specific element of the website using selenium but i want to use css selectors instead of the XPATH i know it have a function to use CSS selector as argument but i want to use this type of selectors

#col-body > div > div.wrapper-info > coin-info-box > div > div > div > div.coin-properties > div:nth-child(16) > div > div.info-text.ng-binding

I got this type of selectors from directly from inspect element and right click and copying the selector directly how can i use those type of selector easily and directly

CodePudding user response:

In order to get the element text you can use this locator as following:

element_text = driver.find_element_by_css_selector('#col-body > div > div.wrapper-info > coin-info-box > div > div > div > div.coin-properties > div:nth-child(16) > div > div.info-text.ng-binding').text

However this locator seems to be very breakable and highly unreliable.
You should learn how to find correct locators, not to use automatically created locators like this.

CodePudding user response:

I am not sure why you've mentioned to not use XPath, and on similar side you are saying to have an option given in pop up .

Instead of this CSS :

#col-body > div > div.wrapper-info > coin-info-box > div > div > div > div.coin-properties > div:nth-child(16) > div > div.info-text.ng-binding

you can simply use :

div.coin-properties > div:nth-child(16)

Xpath you can try is :

//div[text()='BTC']

if you know the text is not going to change, you can use XPath.

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

  • Related