From the HTML code below I want to get the text of 1 and 2 separately.
<div >
<div data-change-key="homeScore.display" >2</div>
<div data-change-key="awayScore.display" >1</div>
</div>
Here is my code:
home_score = driver.find_element(By.CSS_SELECTOR,"div.sc-492bf320-0.sc-7d450bff 9.crIwBV.juiSXn")[0].text
away_score = driver.find_element(By.CSS_SELECTOR,"div.sc-492bf320-0.sc-7d450bff 9.crIwBV.juiSXn")[1].text
I am getting an error that TypeError: 'WebElement'
object is not subscriptable.
What can I change to get the texts separately?
CodePudding user response:
You used find_element
not find_elements
(notice the trailing s
). For the former a single WebElement is returned. For the latter, a List of WebElements is returned. A list is a container and is subscriptable (see more here). A WebElement is not a container like List or Tuple. Hence you cannot use the subscriptable notation [int]
.
Second, div.sc-492bf320-0.sc-7d450bff 9.crIwBV.juiSXn
-- this CSS selector would fail. It would not give you anything. Remove space before 9 and replace it with a dot (.
). More info on using CSS selectors correctly here.