Python 尝试使用Selenium将文件上载到web浏览器
我正在尝试将CSV文件上传到web浏览器的过程自动化。如果单击该按钮,将打开一个windows对话框,选择该文件,并按预期上载该文件。我正在尝试自动化这个小任务 HTML如下所示:Python 尝试使用Selenium将文件上载到web浏览器,python,python-3.x,Python,Python 3.x,我正在尝试将CSV文件上传到web浏览器的过程自动化。如果单击该按钮,将打开一个windows对话框,选择该文件,并按预期上载该文件。我正在尝试自动化这个小任务 HTML如下所示: <input name="csvFile" id="csvFile" class="ignore-inline-attach" type="file"> import requests import selenium from selenium import webdriver from bs4 imp
<input name="csvFile" id="csvFile" class="ignore-inline-attach" type="file">
import requests
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from webbot import Browser
driver = webdriver.Chrome(executable_path= r'C:/Users/ryans/OneDrive/Desktop/Briefcase/Python Scripts/chromedriver.exe')
web = Browser()
url = 'https://web_site'
web.go_to(url)
# 1st validation
web.type('email' , into='username')
web.click('Continue')
# 2nd validation
web.type('email' , into='username')
web.click('Next')
# password
web.type('pass' , into='password')
web.click('Next')
# Now you are logged in!!
url = 'https://web_page'
web.go_to(url)
#################################
# upload CSV Source File:
WebElement = webdriver.find_element_by_class_name('ignore-inline-attach')
WebElement.sendKeys("C:/my_path/test.csv");
#################################
运行此代码时,会收到以下错误消息:
AttributeError: module 'selenium.webdriver' has no attribute 'findElement'
我在网上找到了一些示例脚本,并尝试了一些想法,但什么都没用。我上面发布的两行代码似乎是向前推进的最佳方式,但我似乎无法让它正常工作。这里有人知道怎么做吗?谢谢
dir(webdriver)
# Result:
['ActionChains',
'Android',
'BlackBerry',
'Chrome',
'ChromeOptions',
'DesiredCapabilities',
'Edge',
'Firefox',
'FirefoxOptions',
'FirefoxProfile',
'Ie',
'IeOptions',
'Opera',
'PhantomJS',
'Proxy',
'Remote',
'Safari',
'TouchActions',
'WebKitGTK',
'WebKitGTKOptions',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'__version__',
'android',
'blackberry',
'chrome',
'common',
'edge',
'firefox',
'ie',
'opera',
'phantomjs',
'remote',
'safari',
'support',
'webkitgtk']
引发错误的原因是,按类查找元素名称函数属于webdriver.driver而不是webdriver。请注意,此代码将打开两个浏览器窗口,而不是一个,因为: 基于selenium框架的python Web自动化库 如果查看源代码,您可以看到:
self.driver = webdriver.Chrome(executable_path=driverpath , chrome_options=options)
这意味着您在这两行中打开了两个选项卡:
driver=webdriver.Chromeexecutable_path=r'C:/Users/ryans/OneDrive/Desktop/Briefcase/Python Scripts/chromedriver.exe'
网络=浏览器
你其实不需要这个。可通过以下方式固定:
导入请求
进口硒
从selenium导入webdriver
从bs4导入BeautifulSoup
从webbot导入浏览器
网络=浏览器
url='1〕https://web_site'
web.go\u tourl
第一次验证
在='username'中键入'email'
点击“继续”
第二次验证
在='username'中键入'email'
点击“下一步”
暗语
web.type'pass',into='password'
点击“下一步”
现在您已登录!!
url='1〕https://web_page'
web.go\u tourl
上载CSV源文件:
WebElement=web.driver.find\u element\u by\u class\u name'ignore-inline-attach'
web.driver.sendKeysC:/my_path/test.csv;
关闭浏览器。
web.close\u当前\u标记
很高兴为您提供帮助:使用这些说明,您得到了什么?错误?它什么都没用?请再详细一点。之后你会提交表格吗?你犯了什么错误?哎呀。我忘了加那个部分。我刚刚更新了我的原始帖子。你能试试这行代码吗:WebElement=webdriver。按类查找元素名称‘ignore-inline-attach’我添加了这一行,现在我得到了:AttributeError:module‘selenium.webdriver’没有属性‘按类查找元素名称’