0
votes

We are writing Automation scripts using Selenium Pytest and is trying to get the value in a textbox. The textbox is having a label inside a element (which is used as the locator). However there is another inside and the input is inside the second div (which houses the actual values. There are no built in unique locators available and hence I have used the below locator. Please note that the actual page is having 10 similar textboxes with same HTML as mentioned here, but differing in the textbox names only.

"//label[contains(., 'Textbox')]"

Now I had to click and type on the textbox for which normal selenium click method was returning ElementClickInterceptedException, due to which I had to use ActionChains. ActionChains(driver).move_to_element(driver.find_element_by_xpath(element)).click().send_keys("sample").perform()

Now the issue is that I have to get the value of the data in the textbox. I have tried the below

driver.find_element_by_xpath(element).get_attribute('value')

But it did not work as expected and returned the same ElementNotInteractableException. Please suggest if I can get attribute of the value in the input using any other method. I have already tried the below without much luck, as it returned Nonetype.

driver.find_element_by_xpath(element).text

I have also tried using all other locator combinations, but only the one stated above was found to be working. Please suggest whether there is any way to get the attribute values using ActionChains itself.

Edit #

  1. Adding HTML below as suggested in the comments.

'''

<div class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"><label class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-shrink MuiInputLabel-outlined MuiFormLabel-filled" data-shrink="true" for="mui-76404" id="mui-76404-label">Textbox</label><div class="MuiInputBase-root MuiOutlinedInput-root MuiAutocomplete-inputRoot MuiInputBase-fullWidth MuiInputBase-formControl MuiInputBase-adornedEnd MuiOutlinedInput-adornedEnd"><input aria-invalid="false" autocomplete="off" type="text" class="MuiInputBase-input MuiOutlinedInput-input MuiAutocomplete-input MuiAutocomplete-inputFocused MuiInputBase-inputAdornedEnd MuiOutlinedInput-inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="value" id="mui-76404"><div class="MuiAutocomplete-endAdornment"><button class="MuiButtonBase-root MuiIconButton-root MuiAutocomplete-clearIndicator" tabindex="-1" type="button" aria-label="Clear" title="Clear"><span class="MuiIconButton-label"><svg class="MuiSvgIcon-root MuiSvgIcon-fontSizeSmall" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path></svg></span><span class="MuiTouchRipple-root"></span></button><button class="MuiButtonBase-root MuiIconButton-root MuiAutocomplete-popupIndicator" tabindex="-1" type="button" aria-label="Open" title="Open"><span class="MuiIconButton-label"><svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M7 10l5 5 5-5z"></path></svg></span><span class="MuiTouchRipple-root"></span></button></div><fieldset aria-hidden="true" class="jss229 MuiOutlinedInput-notchedOutline"><legend class="jss231 jss232"><span>Textbox</span></legend></fieldset></div></div>

I have already tried with the below locators without much success. They are working for click action, but is not working when am trying to get attribute.

"//label[contains(., 'Textbox')]/div/input"
"//div[contains(., 'Textbox')]/div/input"
  1. Adding screenshot of empty value getting returned while trying Option # 1 suggested below by @PDHide. enter image description here
1
Please add the HTML of the element you are trying to use .get_attribute('value') on. - Jortega
or just share the URL of the page, makes it easier for the possible helpers - Timeler
@Jortega Added HTML above as suggested - Arun Sasi
can you make a new line per html row or smth? its pretty hard to read that xD - Timeler

1 Answers

1
votes

You are trying to send key to label tag which is not interactable. You have to interact with the input tag. There are two ways to do it :

1. use switch to active element:

locator= "//label[contains(., 'Textbox')]"

textbox = driver.find_element_by_xpath(locator)
textbox.click()

inputElement = driver.switch_to.active_element
inputElement.send_keys("HIHIHIHIHI")

print(inputElement.get_attribute('value'))

print(textbox.text)

2. Directly use the input element:

inputElement = driver.find_element_by_xpath("//label[contains(., 'Textbox')]/..//input")

inputElement.send_keys("HIHIHIHIHI")

print(inputElement.get_attribute('value'))

Output:

enter image description here