4

为什么所有元素的位置都是假的?

browser.divs.collect { |div| p div } #=>
#<Watir::Div:0x6b00922dc26e0b0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fc49d26028c70a780 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x5845c71f58d40810 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fe5d8004222b06fe0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fdabeedaf54ef9220 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fa6b0f134aa2552b0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x7b398ed8727c1a10 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x6960c079dfc53e70 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x3066bfe4aca244c8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x78183373aaf5bb98 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fb97ea89cfbe50c28 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f8516f53906926400 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x15c0a77a1c2b2490 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fb20a77589d504be8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x413b24c40adbaeb8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x5082c3c6fcab0050 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fb5b91eb5d97b6720 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fef34927ce86acbb0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fe355d6cf2c122e80 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fc298449a2ccbef10 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x60c8a26d48ce2e0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x7bbd012d36dc7770 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x5a131e661527aa40 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fce74fbbff21d6ad0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x4ce44bfa8b6695a0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f9a7a3d80975c5230 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x4e515aa604c78900 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x2b9ca16a9c1d4990 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x5af1408ac7077160 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x75f49c45c2e787b8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x584694dea3dd4448 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f93940ef962bb1b18 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x348584082e90dba8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x3144b8ecc252c378 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x4a8cc4f6f0b8308 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f8bd836584d3be6d8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x4f0e9f29854b4b68 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..feb920ae0d472ae38 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fb16c0c9e44203d0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fe145f926e7f266a0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x813352af82b2730 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x5cad2d48230d2e00 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f99c4f7c68d978e90 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fca8a6c43dca7e260 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fe09510792dfca2f0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fad6b90b6dc5eadc0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x295d6bdcf047f018 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x6ff137efc4bcb0a8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fb83092b42841478 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x1f38168bf8c7dfd8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fad6f4b1717935338 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0xe068cbe2682a7c8 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x39333d92a84cbfa0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x8e29f34f5227c30 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x1e716dfa392dd300 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0xf4d3daf64629390 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f8c99737270ec9b60 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..feedea131b3a25bf0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x31d45beea32dbec0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x46a230b8f3dd0350 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x73779048d96d6620 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x47508d81e577c6b0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fa597b94c5c7f3d80 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f854f1b2208e8e10 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x6259997c483ee5e0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fb6bb84691f17a270 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x1a2d6c8537f1940 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x45b47e7cff0f6dd0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x2cae546b88bec0a0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f8f868f3553178130 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x2c67b97a04f98800 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fc0192df0120f4890 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f967334b6fd8d0c60 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fadd345b274c2ccf0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x155ef60e3a24f7c0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x377ad1b63ffdb450 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x19bac99ef08deb20 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..fa4cd04862bbd7fb0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x27a21a5d7aa4d280 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x..f6fea500051d9310 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x3bf03ec6590df6e0 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x69fda612b35d5b70 located=false selector={:element=>(webdriver element)}>
#<Watir::Div:0x180fa81f39a4be40 located=false selector={:element=>(webdriver element)}>

我说是因为有很多次未找到的元素,给出错误。以 Etsy 为例:

b = Watir::Browser.new :chrome
b.goto "https://www.etsy.com/ca/signin?from_page=http%3A%2F%2Fwww.etsy.com%2F&ref=so_sign"
b.text_field(:name, "username").set "Robert" #=> error
b.text_field(:name, "password").set "Robert" #=> error
4

2 回答 2

5

我的解释located=false是 Watir 本身并没有尝试定位元素。当 Watir 创建一个元素集合时,它会获取 selenium 返回的所有元素并将它们映射到新的 Watir 元素对象中。在 Watir 看来,这些新的 Watir 元素对象并没有被“定位”。如果您尝试与元素进行交互,那么定位将是真实的。

但是,我认为这只是 Watir 的实现细节。它似乎不是您的问题的原因。

问题是页面上有 3 个名为“用户名”的文本字段(密码类似)。

browser.text_fields(:name, 'username').each{ |e| puts e.html }
#=> <input autocorrect="off" autocapitalize="off" class="text" name="username" id="username" maxlength="20" type="text">
#=> <input autocorrect="off" autocapitalize="off" class="text" name="username" id="username-existing" tabindex="1" type="text">
#=> <input id="username-existing" autocorrect="off" autocapitalize="off" name="username" value="" class="text " type="text">

看起来您实际上想要与最后一个交互(而不是 Watir 默认选择的第一个)。我相信这很容易通过将搜索范围限定为登录表单中的元素来完成:

form = b.form(:id => 'loginForm')
form.text_field(:id, "username-existing").set "Robert"
form.text_field(:id, "password-existing").set "Robert"
于 2013-09-16T17:03:31.857 回答
0

“元素存在,但位于== false”,通常表示“这样的元素太多了,所以不知道该定位哪一个。所以你需要检查是否有serval windows或serval forms,或serval其他容器。

于 2017-01-10T16:39:23.910 回答