测试必须在时间、质量和成本之间获取一个平衡点,这是测试策略和测试设计的价值体现。
2.26 js解决click失效问题
上一篇 /
下一篇 2018-06-12 10:55:42
/ 个人分类:Python+Selenium2 WebDriver API
2.26 js解决click失效问题
z0hR9x;TT&~.E0前言
P8[A"}4pw
?0有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应。这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了。51Testing软件测试网;HcR.pU)f'`W
本篇用2种方法解决这种诡异的点击事件失效问题51Testing软件测试网R9l8E#lDxOD%W$?
一、遇到的问题51Testing软件测试网VcFg$C
1.在练习百度的搜索设置按钮时,点保存设置按钮,alert弹出没弹出(代码没报错,只是获取alert失败),相信不只是我一个人遇到过。51Testing软件测试网6l3fNk@&R%~
51Testing软件测试网&SH? Fi.q51Testing软件测试网 P
z#D3O?;RC'BokEo
1G(oNH p/C0二、点击父元素
.rI3Y5i"y*qL01.遇到这种问题,应该是前面操作select后导致的后遗症(因为我注释掉select那段是可以点击成功的)。51Testing软件测试网*})Xk.Bx"W_Ba
2.第一种解决办法,先点击它的父元素一次,然后再点击这个元素。51Testing软件测试网3mn*o&NC*k-h
51Testing软件测试网n+a$I'rp4ye51Testing软件测试网/A0Q
p'P*fI3X
51Testing软件测试网RB6i^4f(NX"}3.实现代码如下
:}m8X'b s0h;DU6~Yo pA|0
I'V)`GG4q7XJ \051Testing软件测试网-V;VY,T&C三、js直接点击
M;^[c;UqF%u05lB/GMCP9OO01.遇到这种诡异问题,是时候出绝招了:js大法。51Testing软件测试网!we([o$d;^.wmh
2.用js直接执行点击事件。51Testing软件测试网$MVT'E"Al
51Testing软件测试网 ~go1f2q m:ziR#L5IJ8x)uRH)E08JB8\xr/[0四、参考代码
0c0[p)U*n;C(B/]0#coding:utf-8fromseleniumimportwebdriverfromselenium.webdriver.common.action_chainsimportActionChainsfromselenium.webdriver.support.selectimportSelectimporttime
driver=webdriver.Firefox()
url="https://www.baidu.com"driver.get(url)
time.sleep(3)
mouse= driver.find_element("link text","设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(3)
driver.find_element("link text","搜索设置").click()
time.sleep(3)
s= driver.find_element("id","nr")
Select(s).select_by_visible_text("每页显示50条")#方法一:先点父元素 交流QQ群:232607095#driver.find_element("id", "gxszButton").click()#driver.find_element("class name", "prefpanelgo").click()#方法二:用js直接去点击 交流QQ群:232607095js ='document.getElementsByClassName("prefpanelgo")[0].click();'driver.execute_script(js)
收藏
举报
TAG: