在 Selenium Python 中使用 ActionChains 获取属性

Get attribute using ActionChains in Selenium Python

我们正在使用 Selenium Pytest 编写自动化脚本,并试图在文本框中获取值。文本框在元素内有一个标签(用作定位器)。然而,还有另一个内部,输入在第二个 div 内部(其中包含实际值。没有可用的内置唯一定位器,因此我使用了下面的定位器。请注意实际页面有 10与此处提到的具有相同 HTML 的相似文本框,但仅文本框名称不同。

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

现在我必须单击并键入普通 selenium 单击方法返回的文本框 ElementClickInterceptedException,因此我必须使用 ActionChains。 ActionChains(driver).move_to_element(driver.find_element_by_xpath(element)).click().send_keys("sample").perform()

现在的问题是我必须获取文本框中数据的值。我试过以下

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

但它没有按预期工作并返回相同的 ElementNotInteractableException。请建议我是否可以使用任何其他方法获取输入中值的属性。我已经尝试了下面的方法,但运气不佳,因为它返回了 Nonetype。

driver.find_element_by_xpath(element).text

我也尝试过使用所有其他定位器组合,但发现只有上述组合有效。请建议是否有任何方法可以使用 ActionChains 本身获取属性值。

编辑#

  1. 按照评论中的建议在下面添加 HTML。

'''

<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>

我已经尝试使用以下定位器,但没有成功。他们正在为点击操作工作,但在尝试获取属性时不工作。

"//label[contains(., 'Textbox')]/div/input"
"//div[contains(., 'Textbox')]/div/input"
  1. 添加在尝试@PDHide 下面建议的选项#1 时返回空值的屏幕截图。

您正在尝试将密钥发送到不可交互的标签标签。您必须与输入标签进行交互。有两种方法可以做到:

1.使用切换到活动元素:

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。直接使用输入元素:

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

inputElement.send_keys("HIHIHIHIHI")

print(inputElement.get_attribute('value'))

输出: