所以我有一个类来包装 WebElement 并添加功能以使其在我的页面对象中更有用。我现在需要设置文本输入的文本,而我能看到的唯一方法是注入(inject) javascript 来查找元素并设置其 value 属性。
对于我有一个要关闭的 id 的简单情况,这相当简单,但是如果有动态生成的表单,则可能没有一个有用的 id 可以关闭 - 其中一些是通过查找封闭的表单然后找到的在其中寻找一个 css 选择器,其中还有其他输入与其他表单中的相同 css 选择器匹配。
我可以看到我可以通过 xpath 查找所有内容来解决这个问题,但是必须有一个不那么脆弱/破坏性的解决方案。我知道您可以从 Javascript 执行器获取 WebElement,我想知道是否可以注入(inject)对给定 WebElement 的 javascript 对象引用,如果不能,是否有一种在运行时从现有 WebElement 获取唯一 xpath 的方法.
我使用的是 python,但其他语言的解决方案可能是可移植的。
<小时/>编辑
我知道某处有一个 webdriver 元素缓存,并且我可以在该缓存中获取元素的 id:
有什么方法可以在注入(inject)的 JavaScript 中使用它吗?
最佳答案
如果我正确理解你的问题,你是在问如何将 Selenium WebElement 对象传递给 JavaScript。如果被测试的页面使用 jQuery,它可能看起来像这样:
# select the element in python
form1 = driver.find_element_by_id('id_of_the_form')
# execute javascript which references the object
execute_script("$(arguments[0]).find('input.childOfInterest').val('newValue')", form1)
因为 execute_script()
需要一个字符串(要执行的 JS)和一个可选的第二个参数(对象引用),因此您可以使用 arguments 引用该对象(在本例中为 WebElement) [0]
。在这个例子中,我传入了父表单元素,然后使用 jQuery 选择表单,使其成为 jQuery 对象,然后调用 jQuery 的 .find()
,传入一个 css选择器来定位子元素,然后调用 jQuery 的 .val()
来更新元素的值。
您还询问了如何在运行时生成 xpath。 这是一个示例(完整答案 here ):
// javascript
function getPathTo(element) {
if (element.id!=='')
return 'id("'+element.id+'")';
if (element===document.body)
return element.tagName;
var ix= 0;
var siblings= element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling= siblings[i];
if (sibling===element)
return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
if (sibling.nodeType===1 && sibling.tagName===element.tagName)
ix++;
}
}
以下是在 Selenium 中实现它的步骤
# define xpath generation JS in python
xpathJS = "function getPathTo(element){ if(element....."
# add new function to the page
execute_script(xpathJS)
# select an element with SE
element1 = driver.find_element_by_id('id_of_element')
# get the xpath of the element
xpath1 = execute_script("return getPathTo(arguments[0])", element1)
关于javascript - 获取WebElement的javascript选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21261289/