【问题标题】:Get attribute using ActionChains in Selenium Python在 Selenium Python 中使用 ActionChains 获取属性
【发布时间】:2021-03-12 11:33:40
【问题描述】:

我们正在使用 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. 按照 cmets 中的建议在下面添加 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 时返回的空值的屏幕截图。

【问题讨论】:

  • 请添加您尝试使用的元素的 HTML .get_attribute('value') on。
  • 或者只是分享页面的URL,让可能的帮助者更容易
  • @Jortega 按照建议在上面添加了 HTML
  • 你能在每个 html 行或 smth 中创建一个新行吗?它很难读 xD

标签: python selenium selenium-webdriver automation pytest


【解决方案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'))

输出:

【讨论】:

  • 请参阅 HTML 中的以下部分..
  • 那么问题中添加的html是什么?
  • 它也包含在问题的 HTML 中以及@PDHide。请完整浏览 HTML。
  • 我已经直接从问题中的 HTML 复制了 cmets 中的 HTML sn-p
  • fwiw 命名标签变量文本框有点令人困惑,实际上它是标签。
猜你喜欢
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
相关资源
最近更新 更多