Home > Mobile >  Tagging an HTML element in Python not working
Tagging an HTML element in Python not working

Time:06-22

I have a script going using Selenium and on a new window pop up I am trying to retrieve this element:

<div >somenumber</div>

The full path to this class is

html/body.modal-open/div#modalUserPRMLead.modal.fade.show/div.modal-dialog/div.modal-content/div.modal-body/div.row.justify-content-center.p-3/div.col-md-12/div.custom-control.p-2

I have tried variants of these to grab just the text of 'somenumber'

driver.find_element(By.CSS_SELECTOR,'row justify-content-center p-3').get_attribute('innerHTML')
driver.find_element(By.XPATH("html/body.modal-open/div.col-md-12./div.custom-control.p-2")).text
driver.find_element(By.XPATH("custom-control p-2")).text
driver.find_element(By.XPATH("div.row.justify-content-center.p-3")).text
driver.find_element(By.XPATH,custom-control p-2).text
driver.find_element(By.XPATH,div.custom-control.p-2).text

The errors I get is that it cannot locate the tag from current session. It's a new window that pops up as a result of previous script, I did driver.current_url just to make sure it was capturing the link to the new window in current session and it was.

I'm not understanding how to index down to the div class and just grab the text. Any help would be greatly appreciated. The script prior to window pop for form input was much easier because I could tag everything using By.NAME or By.ID

CodePudding user response:

Your CSS selector is wrong. XPaths don't really looks like XPaths...

Try

driver.find_element(By.XPATH, '//div[@]').text

or

driver.find_element(By.CLASS_NAME, 'custom-control').text

In case element is dynamic you might need to apply wait

You can also check basic syntax of XPath and CSS-selector

  • Related