硒元素不可见异常
Selenium Element not visible exception
我的任务是编写一个解析器来单击网站上的按钮,但我无法仅单击其中一个按钮。以下代码适用于除一个按钮之外的每个按钮。
这是 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()
我遇到了这个错误。
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
根据 Saifur 我刚刚尝试等待相同的元素不可见异常:
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'spsel')][@value='nba']"))).click()
我建议你使用 xpath
和 explicit
等待
//input[contains(@id,'spsel')][@value='nba']
如果您查看页面源代码,您会了解到几乎所有 SELECT
、DIV
元素都是 faked
并从 JavaScript 创建,即这就是为什么 webdriver 不能 SEE 它们。
虽然有一个解决方法,通过使用 ActionChains
打开您的开发者控制台,并在所需元素上注入 人工 点击,实际上,是 Label 触发 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()
或者要替换所有 ActionChains
命令,您可以简单地 运行 execute_script
像这样:
driver.execute_script("document.querySelectorAll('label.boxed')[1].click()")
好了,至少在我的本地文件上...希望这有帮助!
对我有用的是找到有问题的元素之前的元素(也就是说,就 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
这样做之后,我就可以向元素发送动作了。
if "Element is not currently visible" 则使其 可见
f.e.
>>> before is hidden top is outside of page
<input type="file" style="position: absolute;top:-999999" name="file_u">
>>> after move top on in page area
DRIVER.execute_script("document.getElementByName('file_u').style.top = 0;")
time.sleep(1); # give some time to render
DRIVER.find_element_by_name("file_u").send_keys("/tmp/img.png")
您可以尝试 elem = browser.find_element_by_css_selector('#elemId')
而不是 get_element_by_id()
(转到该网页和元素,右键单击它并 Copy CSS Selector
,或类似的东西。)这就是我所做的它有效。您还可以尝试 find_element_by_link_text(text)
、find_element_by_partial_link_text(text)
、find_element_by_tag_name(tagName_case_insensitive_here)
、find_element_by_name(name)
等。有些东西会起作用。在 id
之后,CSS Selector
是您最好的选择。
这个线程的实际解决方案对我不起作用。
然而,
这个做到了:
element = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, xpaths['your_xpath_path'])))
诀窍是使用:
EC.visibility_of_element_located
WebDriverWait
WebDriverWait
从这个进口:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
我最终使用了@twasbrillig 的解决方案,但我没有找到前一个元素并发送 TAB 按键,而是找到了所需的元素,发送带有该元素的 TAB 按键,然后向驱动程序发送 SHIFT + TAB 按键:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
el = driver.find_element_by_id("-spel-nba")
el.send_keys(Keys.TAB)
webdriver.ActionChains(driver).key_down(Keys.SHIFT).send_keys(Keys.TAB).key_up(Keys.SHIFT)
我尝试使用其他方法,但最后发现最简单的方法是尝试单击按钮,然后捕获错误。这使我可以根据它是否有效(真)或无效(假)来执行其他操作。
def click_button(html_object):
try:
html_object.click()
except:
return False #most likely because it is NotVisible object and can be ignored
return True
...
...
click_button(actions)
我在 python
中解决这个问题的方法是:
try:
# the element you want to scroll to
element = driver.find_element_by_id("some_id")
ActionChains(driver).move_to_element(element).perform()
element.send_keys(Keys.TAB).key_up(Keys.SHIFT)
#element.click()
except Exception as e:
print(e)
我的任务是编写一个解析器来单击网站上的按钮,但我无法仅单击其中一个按钮。以下代码适用于除一个按钮之外的每个按钮。
这是 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()
我遇到了这个错误。
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
根据 Saifur 我刚刚尝试等待相同的元素不可见异常:
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'spsel')][@value='nba']"))).click()
我建议你使用 xpath
和 explicit
等待
//input[contains(@id,'spsel')][@value='nba']
如果您查看页面源代码,您会了解到几乎所有 SELECT
、DIV
元素都是 faked
并从 JavaScript 创建,即这就是为什么 webdriver 不能 SEE 它们。
虽然有一个解决方法,通过使用 ActionChains
打开您的开发者控制台,并在所需元素上注入 人工 点击,实际上,是 Label 触发 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()
或者要替换所有 ActionChains
命令,您可以简单地 运行 execute_script
像这样:
driver.execute_script("document.querySelectorAll('label.boxed')[1].click()")
好了,至少在我的本地文件上...希望这有帮助!
对我有用的是找到有问题的元素之前的元素(也就是说,就 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
这样做之后,我就可以向元素发送动作了。
if "Element is not currently visible" 则使其 可见
f.e.
>>> before is hidden top is outside of page
<input type="file" style="position: absolute;top:-999999" name="file_u">
>>> after move top on in page area
DRIVER.execute_script("document.getElementByName('file_u').style.top = 0;")
time.sleep(1); # give some time to render
DRIVER.find_element_by_name("file_u").send_keys("/tmp/img.png")
您可以尝试 elem = browser.find_element_by_css_selector('#elemId')
而不是 get_element_by_id()
(转到该网页和元素,右键单击它并 Copy CSS Selector
,或类似的东西。)这就是我所做的它有效。您还可以尝试 find_element_by_link_text(text)
、find_element_by_partial_link_text(text)
、find_element_by_tag_name(tagName_case_insensitive_here)
、find_element_by_name(name)
等。有些东西会起作用。在 id
之后,CSS Selector
是您最好的选择。
这个线程的实际解决方案对我不起作用。
然而,
这个做到了:
element = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.XPATH, xpaths['your_xpath_path'])))
诀窍是使用:
EC.visibility_of_element_located
WebDriverWait
WebDriverWait
从这个进口:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
我最终使用了@twasbrillig 的解决方案,但我没有找到前一个元素并发送 TAB 按键,而是找到了所需的元素,发送带有该元素的 TAB 按键,然后向驱动程序发送 SHIFT + TAB 按键:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
el = driver.find_element_by_id("-spel-nba")
el.send_keys(Keys.TAB)
webdriver.ActionChains(driver).key_down(Keys.SHIFT).send_keys(Keys.TAB).key_up(Keys.SHIFT)
我尝试使用其他方法,但最后发现最简单的方法是尝试单击按钮,然后捕获错误。这使我可以根据它是否有效(真)或无效(假)来执行其他操作。
def click_button(html_object):
try:
html_object.click()
except:
return False #most likely because it is NotVisible object and can be ignored
return True
...
...
click_button(actions)
我在 python
中解决这个问题的方法是:
try:
# the element you want to scroll to
element = driver.find_element_by_id("some_id")
ActionChains(driver).move_to_element(element).perform()
element.send_keys(Keys.TAB).key_up(Keys.SHIFT)
#element.click()
except Exception as e:
print(e)