Selenium中的简单文件上传代码段适用于Chrome,但不适用于我的Mac上的Firefox

时间:2015-03-07 20:15:43

标签: python google-chrome firefox selenium

this answer提供我已经得到了一些Selenium的工作:

import contextlib
from selenium import webdriver

with contextlib.closing(webdriver.Chrome()) as driver:
    driver.get("http://www.bing.com/images")
    driver.find_element_by_id("sbi_t").click()
    element = driver.find_element_by_id("sbi_file_upload")
    element.send_keys("//Loch Ness Monster.jpg")

但是,当我从webdriver.Chrome()切换到webdriver.Firefox()时,我开始获得selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with。我的猜测是,错误与Selenium执行操作系统文件上传选择器对话框的魔力有关。我认为这可能也是为什么我等待元素变得可见的尝试不起作用:“魔法”并不涉及元素变得可见。这是我试过的:

# https://stackoverflow.com/a/15142611/2829764, found via https://stackoverflow.com/q/6101461/2829764
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "sbi_file_upload")))

我使用的是Firefox 36.0.1,Selenium 2.45.0和Python 2.7.9。顺便提一下,当我将Firefox更新到36.0.1时,Selenium已经停止使用Firefox了,但我今天更新了我的Selenium,我遇到的特殊问题就消失了。

4 个答案:

答案 0 :(得分:2)

您要发送密钥的元素的不透明度值设置为0

<input id="sbi_file_upload" name="imageBinary" type="file" accept="image/*" style="opacity: 0;">

Firefox(正确)认为元素不可见

>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> driver.get("http://www.bing.com/images")
>>> driver.find_element_by_id("sbi_t").click()
>>> element = driver.find_element_by_id("sbi_file_upload")
>>> element.is_displayed()
False

以下是我们看到的行为的实际原因的相关问题:


此处的解决方案是使input可见,以便我们可以将密钥发送给它。我发现了一种让输入可见的相当奇怪的方法 - 它首先发送一个空文件值,然后返回并通过input显示execute_script()。希望,您可以在没有额外back()步骤的情况下使其工作:

>>> driver = webdriver.Firefox()
>>> driver.get('http://www.bing.com/images')
>>> driver.maximize_window()
>>> 
>>> driver.find_element_by_id("sbi_t").click()
>>> 
>>> button = driver.find_element_by_id('sbi_gh')
>>> driver.execute_script('arguments[0].style.display = "block";', button)
>>> button.click()  # submitting the form, empty file
>>> 
>>> driver.back()
>>> driver.find_element_by_id("sbi_t").click()
>>> 
>>> file_input = driver.find_element_by_id('sbi_file_upload')
>>> file_input.is_displayed()
False
>>> 
>>> driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";', file_input)
>>> 
>>> file_input.is_displayed()
True

现在您可以将密钥发送到文件输入 - 经过测试,对我有用。

答案 1 :(得分:2)

@ alexce的答案的轻微修改版本。这是我发现的唯一可以在我的机器上运行的东西。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import selenium

driver = webdriver.Firefox()
driver.get('http://www.bing.com/images')

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "sbi_t")))
driver.find_element_by_id("sbi_t").click()

try:
    WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.ID, "sbi_file_upload")))
    raise Exception("This never seems to happen")
except selenium.common.exceptions.TimeoutException:
    pass

button = driver.find_element_by_id('sbi_gh')
driver.execute_script('arguments[0].style.display = "block";', button)
button.click()  # submitting the form, empty file
driver.back()

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "sbi_t")))
driver.find_element_by_id("sbi_t").click()
file_input = driver.find_element_by_id('sbi_file_upload')
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";', file_input)
file_input.send_keys("//Loch Ness Monster.jpg")

答案 2 :(得分:2)

解决方案'让元素在firefox中可见' 对我来说很棒

driver.execute_script('arguments[0].style = ""; 
arguments[0].style.display = "block"; 
arguments[0].style.visibility = "visible";', file_input)

答案 3 :(得分:0)

试试这个,如果from selenium import webdriver def is_Displayed(class_name): ele=driver.execute_script("return document.getElementsByClassName('"+class_name+"')[0].style.visibility = 'visible';") if ele=="visible": return True else: return False def test_check_element(): global driver driver=webdriver.Safari()//e.g Firefox() , Chrome() driver.maximize_window() driver.implicitly_wait(30) driver.get("url") submitBtn=driver.find_element_by_xpath("//button[@title='Submit']").get_attribute("class") if is_Displayed(submitBtn)== True: print "Displayed " else: print "Not Displayed" 方法给出异常

=IFERROR(INDEX($B$1:$B$6,SMALL(IF($D$1:$D$6="A",ROW($D$1:$D$6)),ROW(1:1))),"")