Home > Back-end >  How to get value using python selenium of element that is not selectable
How to get value using python selenium of element that is not selectable

Time:01-12

I am trying to get a value 570 using Python selenium.

...
<div _ngcontent-lci-c225="" >
    <span _ngcontent-lci-c225=""  style="margin-right: 5px;">Bank</span>
    <!---->
    570
    </div>

I am not sure what is and why when I highlight it in chrome dev tools it doesn't show me anything in styles window where I would usually see something. Tried googling to get the meaning of without success. If anyone knows please explain to me.

My code:

driver.find_element(By.XPATH, "/html/body/app-root/ng-component/div[4]/mat-drawer-container/mat-drawer-content/mat-grid-list/div/mat-grid-tile[2]/figure/app-inventory-menu/div/div[1]/div[2]/div[2]/span").get_attribute('innerHTML')

It returns 'Bank' and not the actual numerical value that I need. Not sure what else I can do?

CodePudding user response:

It's because the number is not contained within the <span> element, but rather the <div> element above it. Based on your code, the best selector I can find for it is the CSS Selector: "div.mr-4.bd-highlight"

CodePudding user response:

parent = driver.find_element(By.CSS_ELECTOR,"mr-4.bd-highlight")
child = parent.find_element(By.TAG_NAME,"span")
print (parent.text.replace(child.text, ''))

Try to remove the span text. I know there's a way better way then this using split.

CodePudding user response:

Thank you for suggestion I was able to figure it out.

driver.find_element(By.CSS_SELECTOR, "div.inventory-row:nth-child(1) > div:nth-child(2) > div:nth-child(2)").text.strip("Bank")

Maybe there is a better way, but it works for me.

  •  Tags:  
  • Related