我的任务是编写解析器来点击网站上的按钮,我只有点击其中一个按钮时遇到问题.以下代码适用于除一个按钮之外的每个按钮.
这是html:http: //pastebin.com/6dLF5ru8
这是源html: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()
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误.
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Run Code Online (Sandbox Code Playgroud)
根据Saifur,我只是尝试使用相同的元素等待不可见的异常:
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'spsel')][@value='nba']"))).click()
Run Code Online (Sandbox Code Playgroud)
Anz*_*zel 42
如果你看一下页面的源代码,你会明白,几乎所有的SELECT
,DIV
都是元素faked
和从JavaScript创建的,这就是为什么webdriver的不能查阅它们.
有一个解决方法,通过使用ActionChains
打开你的开发人员控制台,并在所需的元素上注入一个人工 CLICK,事实上,这是触发NBA数据加载的标签 ......这是一个工作示例:
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()
Run Code Online (Sandbox Code Playgroud)
或者替换所有ActionChains
命令,您可以execute_script
像这样运行:
driver.execute_script("document.querySelectorAll('label.boxed')[1].click()")
Run Code Online (Sandbox Code Playgroud)
你去,至少在我当地的文件上...希望这有帮助!
twa*_*lig 15
对我有用的是在有问题的元素之前找到元素(也就是说,在Tab键顺序方面就在它之前),然后在该元素上调用Tab.
from selenium.webdriver.common.keys import Keys
elem = br.find_element_by_name("username")
elem.send_keys(Keys.TAB) # tab over to not-visible element
Run Code Online (Sandbox Code Playgroud)
在这之后,我能够向元素发送动作.