0
caller = driver.find_element_by_id("sys_display.new_call.caller") 
print(caller.get_attribute('value'))

HTML CODE

Hello running into issues when trying to extract a attribute value from a element. I have tried using caller= driver.find_element_by_id("sys_display.new_call.caller").get_attribute('value') but it doesnt seem to pull the value at all from HTML.

I am a noobie thanks for the help!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Justin.U
  • 11
  • 5

2 Answers2

0

To print the value of the value attribute i.e. MyMercy User you can use either of the following Locator Strategies:

  • Using css_selector:

    print(driver.find_element_by_css_selector("input[id^='sys_display'][id*='new_call'][id$='caller'][data-name='caller']").get_attribute("value"))
    
  • Using xpath:

    print(driver.find_element_by_xpath("//input[@id='sys_display.new_call.caller' and @data-name='caller']").get_attribute("value"))
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[id^='sys_display'][id*='new_call'][id$='caller'][data-name='caller']"))).get_attribute("value"))
    
  • Using XPATH:

    driver.get('https://www.temporary-mail.net/')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='sys_display.new_call.caller' and @data-name='caller']"))).get_attribute("value"))
    
  • Console Output:

    MyMercy User
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @PDHide Let's discuss the issue in details within [Selenium Chat Room](https://chat.stackoverflow.com/rooms/223360/selenium) – undetected Selenium Dec 07 '20 at 14:09
  • So these keep giving me an error of: Traceback (most recent call last): File "c:/Users/jjudi/OneDrive/Documents/Python Scripts/import selenium.py", line 40, in print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[id^='sys_display'][id*='new_call'][id$='caller'][data-name='caller']"))).get_attribute("value")) File "C:\Users\jjudi\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Justin.U Dec 07 '20 at 16:59
  • @Justin.U Pretty much expected as you haven't provided the text based HTML which would have helped us to test our own solution before publishing. – undetected Selenium Dec 07 '20 at 17:07
  • Sorry its not a public site so I dont want to release to much information on it. I do appreciate the help tho, Do I need to specify that its in main? – Justin.U Dec 07 '20 at 17:22
-1

In selenium if the value is stored as innerText, you have to retrive it using .text method , this works only if the element is visible in the UI. Means if its hidden or out of view port .text method will return blank. In that case you could use get attribute "textContent" . But for testing textCOntent is not recommended as it won't validate if the text is displaye in UI. It is recommended only for web scraping

You can also wait for value field to have the text before printing you are indeed looking for value attribute

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
caller = wait.until(EC.text_to_be_present_in_element_value((By.ID, 'sys_display.new_call.caller'),"MyMercy User"))

caller = driver.find_element_by_id("sys_display.new_call.caller") 
print(caller.get_attribute('value'))
print(caller.text)
print(caller.get_attribute('textContent'))

Try printing the three values and see which one gives the result you want

PDHide
  • 18,113
  • 2
  • 31
  • 46