简体   繁体   English

Selenium firefox配置文件永远不会要求不工作

[英]Selenium firefox profile never ask not working

I am trying to save a csv file without the popup box. 我正在尝试保存没有弹出框的csv文件。 I followed the following link to figure out my mime: 我点击了以下链接以找出我的哑剧:

http://watirmelon.com/2011/09/07/determining-file-mime-types-to-autosave-using-firefox-watir-webdriver/ http://watirmelon.com/2011/09/07/determining-file-mime-types-to-autosave-using-firefox-watir-webdriver/

I even added all the NC_values from the mimeTypes.rdf file. 我什至还添加了mimeTypes.rdf文件中的所有NC_values。 But, it does not seem to be working. 但是,它似乎不起作用。

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'irc,ircs,webcal,application/csv,application/download,application/pdf')

mimeTypes.rdf mimeTypes.rdf

<RDF:RDF xmlns:NC="http://home.netscape.com/NC-rdf#"
         xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <RDF:Seq RDF:about="urn:schemes:root">
    <RDF:li RDF:resource="urn:scheme:webcal"/>
    <RDF:li RDF:resource="urn:scheme:ircs"/>
    <RDF:li RDF:resource="urn:scheme:mailto"/>
    <RDF:li RDF:resource="urn:scheme:irc"/>
  </RDF:Seq>
  <RDF:Description RDF:about="urn:scheme:mailto"
                   NC:value="mailto">
    <NC:handlerProp RDF:resource="urn:scheme:handler:mailto"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:handler:web:https://30boxes.com/external/widget?refer=ff&amp;url=%s"
                   NC:prettyName="30 Boxes"
                   NC:uriTemplate="https://30boxes.com/external/widget?refer=ff&amp;url=%s" />
  <RDF:Description RDF:about="urn:scheme:irc"
                   NC:value="irc">
    <NC:handlerProp RDF:resource="urn:scheme:handler:irc"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:handler:mailto"
                   NC:useSystemDefault="true"
                   NC:alwaysAsk="false">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://compose.mail.yahoo.com/?To=%s"/>
    <NC:possibleApplication RDF:resource="urn:handler:web:https://mail.google.com/mail/?extsrc=mailto&amp;url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:handler:web:https://mail.google.com/mail/?extsrc=mailto&amp;url=%s"
                   NC:prettyName="Gmail"
                   NC:uriTemplate="https://mail.google.com/mail/?extsrc=mailto&amp;url=%s" />
  <RDF:Description RDF:about="urn:scheme:handler:webcal"
                   NC:useSystemDefault="true"
                   NC:alwaysAsk="true">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://30boxes.com/external/widget?refer=ff&amp;url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:application/pdf"
                   NC:value="application/pdf">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:application/pdf"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:ircs"
                   NC:value="ircs">
    <NC:handlerProp RDF:resource="urn:scheme:handler:ircs"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:handler:ircs"
                   NC:alwaysAsk="true">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://www.mibbit.com/?url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:handler:application/download"
                   NC:alwaysAsk="true" />
  <RDF:Description RDF:about="urn:mimetype:application/csv"
                   NC:value="application/csv"
                   NC:editable="true"
                   NC:fileExtensions="csv"
                   NC:description="Microsoft Excel Comma Separated Values File">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:application/csv"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:schemes">
    <NC:Protocol-Schemes RDF:resource="urn:schemes:root"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:handler:web:https://compose.mail.yahoo.com/?To=%s"
                   NC:prettyName="Yahoo! Mail"
                   NC:uriTemplate="https://compose.mail.yahoo.com/?To=%s" />
  <RDF:Description RDF:about="urn:handler:web:https://www.mibbit.com/?url=%s"
                   NC:prettyName="Mibbit"
                   NC:uriTemplate="https://www.mibbit.com/?url=%s" />
  <RDF:Seq RDF:about="urn:mimetypes:root">
    <RDF:li RDF:resource="urn:mimetype:application/pdf"/>
    <RDF:li RDF:resource="urn:mimetype:application/download"/>
    <RDF:li RDF:resource="urn:mimetype:application/csv"/>
  </RDF:Seq>
  <RDF:Description RDF:about="urn:scheme:handler:irc"
                   NC:alwaysAsk="true">
    <NC:possibleApplication RDF:resource="urn:handler:web:https://www.mibbit.com/?url=%s"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:handler:application/csv"
                   NC:alwaysAsk="true" />
  <RDF:Description RDF:about="urn:mimetypes">
    <NC:MIME-types RDF:resource="urn:mimetypes:root"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:application/download"
                   NC:value="application/download"
                   NC:editable="true"
                   NC:fileExtensions="csv"
                   NC:description="Microsoft Excel Comma Separated Values File">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:application/download"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:scheme:webcal"
                   NC:value="webcal">
    <NC:handlerProp RDF:resource="urn:scheme:handler:webcal"/>
  </RDF:Description>
  <RDF:Description RDF:about="urn:mimetype:handler:application/pdf"
                   NC:handleInternal="true"
                   NC:alwaysAsk="false" />
  <RDF:Description RDF:about="urn:root"
                   NC:en-US_defaultHandlersVersion="4" />
</RDF:RDF>

my full code: 我的完整代码:

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/csv,application/download')

driver = webdriver.Firefox(profile)
driver.get(url)

time.sleep(5)

driver.switch_to.frame("main")
elem = driver.find_element_by_name('username')
elem.send_keys(username)
elem = driver.find_element_by_name("password")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)

driver.find_element_by_xpath(".//*[@id='wrap']/div/div/div/div/div/div/div/div/div/div/div[2]/div/div/div/div[1]/div/span/span[2]/div[2]/table/tbody/tr/td[2]").click()
time.sleep(10)
driver.find_element_by_xpath(".//*[@id='scrollable']/div/div/div[1]/ul/li[1]/ul/li[2]/a[2]").click()
time.sleep(10)

#download report
try:
    driver.switch_to.window(driver.window_handles[1])
    element = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,".//*[@id='datablock']/div[5]/div[1]/div[10]/div[12]/input")))
    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[12]/input").clear()
    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[12]/input").send_keys(fromDate)

    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[13]/input").clear()
    driver.find_element_by_xpath(".//*[@id='datablock']/div[5]/div[1]/div[10]/div[13]/input").send_keys(toDate)

    driver.find_element_by_xpath(".//*[@id='datablock']/div[1]/div[2]/a[2]").click()
    driver.find_element_by_xpath("html/body/div[8]/ul/li/span").click()
    driver.find_element_by_xpath(".//*[@id='datablock']/div[1]/div[2]/a[3]").click()
except:
        print 'error'

magic Python module says it's text/plain : magic Python模块说它是text/plain

>>> import magic
>>> mime = magic.Magic(mime=True)
>>> mime.from_file("report.csv")
'text/plain'

The final preference (also leaving application/csv and application/download and adding application/octet-stream and text/csv to be safe): 最终的首选项(还保留了application/csvapplication/download并添加了application/octet-streamtext/csv是安全的):

profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain,text/csv,application/csv,application/download,application/octet-stream')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM