【发布时间】:2017-11-07 15:25:40
【问题描述】:
我在python中使用这个函数从当前打开的页面中选择随机链接并点击它:
def click_random_link(driver):
print("Clicking random link ")
links = driver.find_elements_by_xpath("//*[@href]")
if len(links):
l = links[randint(0, len(links) - 1)]
print(l.get_attribute("href"))
sleep(1)
l.click()
return True
else:
print('Link NOT found....')
return False
看起来很简单,但它非常非常不可靠..在超过 80% 的时间内我会得到 p>
errorMessage":"Element is not currently visible and may not be manipulated"
无论我在 phantomjs 中尝试什么,我都无法让它可靠地点击链接。我尝试最大化窗口,尝试聚焦链接,但没有任何效果:( 最后一枪在这里变暗,但即使是全能的谷歌没有答案...
【问题讨论】:
-
您是否尝试在通过 xpath 定位元素后将链接滚动到视图中?通常,我发现广告/页眉/页脚可以悬停在您尝试点击的链接上
-
我已尝试添加此链接以聚焦链接,但结果大多相同,并非 100% 可靠。
action = ActionChains(driver) action.move_to_element(l).click().perform() -
看来我得把它拿回来 :D,现在运行测试,到目前为止,move_to_element 都很好