Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium Webdriver-PhantomJS在将_keys()发送到文件输入元素时挂起_Python_Browser_Selenium Webdriver_Automation - Fatal编程技术网

Python Selenium Webdriver-PhantomJS在将_keys()发送到文件输入元素时挂起

Python Selenium Webdriver-PhantomJS在将_keys()发送到文件输入元素时挂起,python,browser,selenium-webdriver,automation,Python,Browser,Selenium Webdriver,Automation,我正在执行一些文件上传测试。我发现如果我使用PhantomJS,我的测试代码挂起在元素。发送密钥(文件),但是如果我使用Firefox,相同的代码不会挂起 element = self.browser.find_element_by_xpath("//input[@type='file']") element.send_keys(file) 有没有什么办法可以让PhantomJS正确上传文件?目前我正在使用Windows7、Python 3.4.1、selenium 2.42.1、Phanto

我正在执行一些文件上传测试。我发现如果我使用PhantomJS,我的测试代码挂起在
元素。发送密钥(文件)
,但是如果我使用Firefox,相同的代码不会挂起

element = self.browser.find_element_by_xpath("//input[@type='file']")
element.send_keys(file)
有没有什么办法可以让PhantomJS正确上传文件?目前我正在使用Windows7、Python 3.4.1、selenium 2.42.1、PhantomJS 1.9.7

browser = webdriver.PhantomJS()
browser.set_window_size(1200,800)

如果不设置窗口大小,浏览器将保持移动大小,从而导致错误。请尝试隐式等待。

应使用PhantomJS.uploadFile()。但是,没有找到python selenium API

var webPage = require('webpage');   
var page = webPage.create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');

奇怪的是,我无法在我的ubuntu外壳中运行任何东西,但它将通过Jupyter笔记本上的iPython在完全相同的服务器上运行

我必须在代码中添加一个虚拟显示,使其作为.py脚本从shell运行

如果它能帮助任何面临类似问题的人,那么我添加到脚本中的代码行和send键就可以毫无问题地开始工作了

from pyvirtualdisplay import Display

# Set screen resolution to 1366 x 768 like most 15" laptops. This is needed 
#to run in the shell. Seems fine in iPython
display = Display(visible=0, size=(1366, 768))
display.start()

当我不能简单地更改文件输入标记的值时,我使用这种方法

我们将在控制台上运行它,因此我们应该使用window manager(我使用的是dwm,您可以尝试fluxbox,它很容易安装,但需要比dwm更多的RAM)和Xephyr进行调试。 要运行窗口管理器,我们将使用

所以我们将调用文件上传对话框,通过点击元素或按钮,然后我们模拟发送键,所有这些都在控制台中运行

import time

from easyprocess import EasyProcess
from pynput.keyboard import Key, Controller
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class ImageUploadAutomation:
    chrome = 'path/to/chrome'
    chrome_options = Options()
    # chrome_options.add_argument('--headless')
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-notifications")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--window-size=1280,900")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_argument("user-data-dir=selenium")
    chrome_options.add_experimental_option(
        "excludeSwitches", ["disable-popup-blocking"]
    )
    chrome_options.add_argument(
        "--disable-blink-features=AutomationControlled"
    )

    driver = None

    def upload(self, photo):
        # Change visible to 1 if you want to use Xephyr debug
        with Display(visible=0, size=(1280, 900)) as display:
            with EasyProcess(["dwm"]) as process:
                keyboard = Controller()
                url = "https://www.exmaple.com"

                self.driver = webdriver.Chrome(
                    executable_path=self.chrome,
                    chrome_options=self.chrome_options
                )
                self.driver.get(url)

                test = self.driver.find_element_by_xpath(
                    "//div[@aria-label='Add Photos']"
                )
                time.sleep(1)
                test.click()

                time.sleep(1)

                for key in photo.path:
                    keyboard.press(key)
                    keyboard.release(key)

                # keyboard.press(Key.enter)
                with keyboard.pressed(Key.enter):
                    pass

你能告诉我输入是否有一个muliplete属性吗输入确实有“multiple”属性。当你发送.keys(文件)时,我对PhantomJS也有同样的问题。我没有任何进展的运气,我可以说selenium并没有把html5页面做得那么好。我们无法使用send_键直接设置输入值。我们需要打开upload browser(上传浏览器)对话框窗口(如果您有),并使用外部自动化API(如AutoIT等)填写文件路径,因为webdriver根本不知道对话框窗口在OS doamin级别下。我知道这很麻烦,但我尝试过执行javascript,Action builder都不起作用。此外,如果可能的话,尝试使用不同的浏览器,如chrome或firefox,因为phantomjs是无头浏览器。即使没有“multiple”元素,也会发生这种情况