Home > Mobile >  How to get text element in html head by selenium?
How to get text element in html head by selenium?

Time:11-02

I am trying to get a file name of a jpg from this link by selenium (Firefox driver). If I manually right click on the photo and download it, the file name will be 1-002.jpg

When I try to browse the html by Firefox, it shows that 1-002.jpg is located in the <'head'> <'title'>section. This can't be shown on chrome for some reason.

<Head>
 <meta name="viewport" content="width=device-width; height=device-height;">
 <link rel="stylesheet" href="resource://content-accessible/ImageDocument.css">
 <link rel="stylesheet" href="resource://content-accessible/TopLevelImageDocument.css">
 <link rel="stylesheet" href="chrome://global/skin/media/TopLevelImageDocument.css">
 <title>1-002.jpg (JPEG Image, 1800&nbsp;×&nbsp;2546 pixels) — Scaled (40%)</title>
</Head>

However, I am unable to get the text. So far my codes are below

driver.get(url)

file_name = driver.find_element_by_xpath('/html/head/title')
print(file_name)

I can only get <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="384e7def-df66-4f06-9c3a-c5072c2d650f", element="7e0002d0-7bfb-4aed-8558-fdb9c25075d4")>

if I replace it by file_name = driver.find_element_by_xpath('/html/head/title').text , it will return nothing.

So I am asking how can I get the wanted text.

Thank you.

CodePudding user response:

First you will have to check in HTMLDOM that we have unique entry or not :

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.

For firefox please do inspect and steps should remain same.

If /html/head/title is 1/1, then the issue could be cause of delay.

Hardcoded sleep (Not recommended)

Code trial 1 :

time.sleep(5)
file_name = driver.find_element_by_xpath('/html/head/title').text
print(file_name)

Code trial 2 : (Recommended)

file_name =  WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/head/title"))).text
print(file_name)

Other things you can try out :

Please call .get_attribute('innerText') or .get_attribute('innerHTML') instead of .text, like this :

With innerHTML

file_name =  WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/head/title"))).get_attribute('innerHTML')
print(file_name)

With innerText

file_name =  WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/head/title"))).text
print(file_name)get_attribute('innerText')

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • Related