Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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
如何在Selenium和Python中选择上载按钮并指定文档上载路径_Python_Selenium_Selenium Webdriver_Selenium Chromedriver_Browser Automation - Fatal编程技术网

如何在Selenium和Python中选择上载按钮并指定文档上载路径

如何在Selenium和Python中选择上载按钮并指定文档上载路径,python,selenium,selenium-webdriver,selenium-chromedriver,browser-automation,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,Browser Automation,因此,我必须在单击上一页上的编辑按钮后,在从新页面打开的页面上选择上载按钮。当它打开一个新页面时,我必须选择要上载的文件,方法是选择“以下是我尝试选择该元素时得到的回溯”: Traceback (most recent call last): File ".\skyward_collegeboard_TSI_import.py", line 143, in <module> element = WebDriverWait(browser, 20).until( Fil

因此,我必须在单击上一页上的编辑按钮后,在从新页面打开的页面上选择上载按钮。当它打开一个新页面时,我必须选择要上载的文件,方法是选择“以下是我尝试选择该元素时得到的回溯”:

Traceback (most recent call last):
  File ".\skyward_collegeboard_TSI_import.py", line 143, in <module>
    element = WebDriverWait(browser, 20).until(
  File "C:\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
如前所述,我尝试了
NAME
XPATH
、和
ID
,但它们都没有成功。 我甚至试着写一行选择这个按钮,这样我就可以进入下一步上传,然后点击保存

更新:尝试切换到该窗口,因为总共有三个窗口。此外,我尝试了@supputuri下面提到的答案,并得到了以下回溯

  File ".\skyward_collegeboard_TSI_import.py", line 151, in <module>
    element.send_keys("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett
  (Session info: chrome=80.0.3987.122)
以下是我需要从Selenium IDE录制中与之交互的特定元素:

{
      "id": "2193d953-a105-45d0-8e6f-7fa910863cc2",
      "comment": "",
      "command": "click",
      "target": "id=file1",
      "targets": [
        ["id=file1", "id"],
        ["name=file1", "name"],
        ["css=#file1", "css:finder"],
        ["xpath=//input[@id='file1']", "xpath:attributes"],
        ["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
        ["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "2f5cfebb-5e97-42da-97c6-2f7880fc2ec5",
      "comment": "",
      "command": "type",
      "target": "id=file1",
      "targets": [
        ["id=file1", "id"],
        ["name=file1", "name"],
        ["css=#file1", "css:finder"],
        ["xpath=//input[@id='file1']", "xpath:attributes"],
        ["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
        ["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
      ],
      "value": "C:\\fakepath\\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett.csv"
    }, 

将文件路径发送到元素

element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.NAME, "file1")))
# don't click on the element, just send the path directly
element.send_keys("File/path/goes/here")

是的@supputuri它似乎不起作用,我得到了类似于前面提到的追踪的东西<代码>回溯(最后一次调用):文件“\skyward\u collegeboard\u TSI\u import.py”,第144行,在element=WebDriverWait(浏览器,20)中。直到(EC.element\u to\u be\u clickable((By.NAME,“file1”))文件“C:\Python38\lib\site packages\selenium\webdriver\support\wait.py”,第80行,直到raise TimeoutException(消息、屏幕、堆栈跟踪)selenium.common.exceptions.TimeoutException:Message:使用XPATH尝试了同样的方法,但也不起作用。在我将元素设置为启用后,我可以使用上面的代码使其起作用。因此我尝试了@supputuri对答案的建议,但是,我发现我没有切换到该窗口页面,但是我仍然有选择元素“File”.\skyward\u collegeboard\u TSI\u import.py“,第151行,在element.send\u key(“C:\Imports\customnameedsneedsformating\u 02\u 24\u 2020\u 20\u 14\u 12\u richardberrett”)文件“C:\Python38\lib\site packages\selenium\webdriver\remote\webelement.py”中的问题,我认为这是send\u key的一个错误
# Upload Test File
# Choose File Button XPATH = //*[@id='file1']
element = WebDriverWait(browser, 20).until(
            EC.element_to_be_clickable((By.NAME, "file1")))
element.click();
  File ".\skyward_collegeboard_TSI_import.py", line 151, in <module>
    element.send_keys("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett
  (Session info: chrome=80.0.3987.122)
# Upload Test File
# Choose File Button XPATH = //*[@id='file1']
# Window Page Address that Opens = https://skyward-student.del-valle.k12.tx.us/scripts/wsisa.dll/WService=wsEAplus/simptedit000.w?isPopup=true
# Browser Switches to Window
WebDriverWait(browser,10).until(EC.number_of_windows_to_be(3))
browser.switch_to.window(browser.window_handles[-1])
#upload = browser.find_element_by_id("file1")
#upload.send_keys(r"C:\Users\richard.barrett\Downloads\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
element = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='file1']")))
element.send_keys("C:\Imports\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett")
{
      "id": "2193d953-a105-45d0-8e6f-7fa910863cc2",
      "comment": "",
      "command": "click",
      "target": "id=file1",
      "targets": [
        ["id=file1", "id"],
        ["name=file1", "name"],
        ["css=#file1", "css:finder"],
        ["xpath=//input[@id='file1']", "xpath:attributes"],
        ["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
        ["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "2f5cfebb-5e97-42da-97c6-2f7880fc2ec5",
      "comment": "",
      "command": "type",
      "target": "id=file1",
      "targets": [
        ["id=file1", "id"],
        ["name=file1", "name"],
        ["css=#file1", "css:finder"],
        ["xpath=//input[@id='file1']", "xpath:attributes"],
        ["xpath=//div[@id='pageContentWrap']/table/tbody/tr/td/fieldset/table/tbody/tr[9]/td/fieldset/table/tbody/tr/td[2]/table/tbody/tr/td/input", "xpath:idRelative"],
        ["xpath=//td[2]/table/tbody/tr/td/input", "xpath:position"]
      ],
      "value": "C:\\fakepath\\CustomNameNeedsFormatting_02_24_2020_20_14_12_richardbarrett.csv"
    }, 
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.NAME, "file1")))
# don't click on the element, just send the path directly
element.send_keys("File/path/goes/here")