Selenium中Firefox和IE的包含onclick的CSS定位器

0 投票
1 回答
880 浏览
提问于 2025-04-17 10:34

我正在使用最新的Selenium RC Python客户端和Selenium RC服务器。我想在一个网页上点击一个按钮,这个按钮的HTML标签如下:

<input type="submit" id="b1" value="Button1 - Standard" onclick="javascript: alert('button1')">

下面的Python代码使用CSS定位器xxx在Firefox上点击了上面的按钮,但在IE上却没有。

在Firefox上:

from selenium import selenium
s1 = selenium('myhost_ip_address', 4444, '*chrome', 'http://mypage_with_button.com')
print s1.is_element_present('css=input[value="Button1 - Standard"][type="submit"][id="b1"][onclick="javascript: alert(\'button1\')"]')
# Returns True

在IE8上:

# NOTE: On IE, you might have to click on the notification 'To help protect your security... ActiveX controls that could....' and Allow Blocked Contents if needed
from selenium import selenium
s1 = selenium('myhost_ip_address', 4444, '*chrome', 'http://mypage_with_button.com')
print s1.is_element_present('css=input[value="Button1 - Standard"][type="submit"][id="b1"][onclick="javascript: alert(\'button1\')"]')
# Returns False

我完全知道这个CSS有唯一的ID,我不需要像type和onclick这样的属性(我只是好奇为什么会这样)。

总之,这篇帖子主要是关于一个Selenium CSS定位器,它有onclick作为属性,在Firefox上能用,但在IE上却不行。有没有人知道这是为什么呢?

1 个回答

0

我在使用定位器的时候,遇到过关于onclick属性的麻烦。我的结论是,当浏览器渲染HTML后,onclick属性会变成一个JavaScript函数对象,这样通过文本模式去匹配就会失败。我也不知道为什么在Firefox中可以用,而在IE中不行。可能是一个浏览器使用了原生的CSS选择器评估,而另一个则使用了注入的Sizzle引擎。

撰写回答