硒元素不可见异常 [英] Selenium Element not visible exception

查看:58
本文介绍了硒元素不可见异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是编写一个解析器来单击网站上的按钮,但我无法仅单击其中一个按钮.以下代码适用于除一个按钮之外的每个按钮.

I have been tasked with writing a parser to click a button on a website and I am having issues to click only one of the buttons. The following code works on every button except one.

这是html:http://pastebin.com/6dLF5ru8

这是源代码:http://pastebin.com/XhsedGLb

python 代码:

 driver = webdriver.Firefox()  
 ...
 el = driver.find_element_by_id("-spel-nba")
 actions.move_to_element(el)
 actions.sleep(.1)
 actions.click()
 actions.perform()

我收到此错误.

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

根据 Saifur,我只是尝试使用相同的元素不可见异常等待:

as per Saifur I just tried waits with the same element not visible exception:

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'spsel')][@value='nba']"))).click()

推荐答案

如果你看一下页面源码,你就会明白几乎所有的SELECTDIV 元素是伪造的,并且是从 JavaScript 创建的,这就是 webdriver 无法看到它们的原因.

If you look at the page source, you'll understand that almost all of theSELECT, DIV elements are faked and created from JavaScript, that is why webdriver cannot SEE them.

不过,有一个解决方法,通过使用 ActionChains 打开您的开发者控制台,并在所需元素上注入一个人工 CLICK,这实际上就是 标签触发NBA数据加载......这是一个工作示例:

There's a workaround though, by using ActionChains to open your developer console, and inject an artificial CLICK on the desired element, which in fact, is the Label triggering the NBA data loading... here's a working example:

from selenium import webdriver
from selenium.webdriver.common import action_chains, keys
import time

driver = webdriver.Firefox()
driver.get('Your URL here...')
assert 'NBA' in driver.page_source
action = action_chains.ActionChains(driver)

# open up the developer console, mine on MAC, yours may be diff key combo
action.send_keys(keys.Keys.COMMAND+keys.Keys.ALT+'i')
action.perform()
time.sleep(3)
# this below ENTER is to rid of the above "i"
action.send_keys(keys.Keys.ENTER)
# inject the JavaScript...
action.send_keys("document.querySelectorAll('label.boxed')[1].click()"+keys.Keys.ENTER)
action.perform()

或者替换所有 ActionChains 命令,你可以简单地运行 execute_script 像这样:

Alternatively to replace all the ActionChains commands, you can simply run execute_script like this:

driver.execute_script("document.querySelectorAll('label.boxed')[1].click()")

你去吧,至少在我的本地文件中...希望这会有所帮助!

There you go, at least on my local file anyway... Hope this helps!

这篇关于硒元素不可见异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆