Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

如何在Selenium和Python中使用类型查找元素

如何在Selenium和Python中使用类型查找元素,python,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,我有下面的html代码 <div align="center"> <input type="file" name="filePath"><br> <input type="Submit" value="Upload File"><br> </div> 我正在尝试使用Selenium和Python查找两个元素“file”和“submit”。下面是我尝试使用的代码 from selenium impor

我有下面的html代码

<div align="center">
    <input type="file" name="filePath"><br>
    <input type="Submit" value="Upload File"><br>
</div>



我正在尝试使用Selenium和Python查找两个元素“file”和“submit”。下面是我尝试使用的代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# create a new Firefox session
driver = webdriver.Chrome()

# Maximize the browser window
driver.maximize_window()

# Enter the url to load
driver.get("<<MY PAGE TO LOAD>>")

# Wait for the page to load
driver.implicitly_wait(5)

# find the upload file type and pass a test value
upload_field = driver.find_element_by_partial_link_text('file')
upload_field.clear()
upload_field.send_keys("test")
从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
#创建新的Firefox会话
driver=webdriver.Chrome()
#最大化浏览器窗口
驱动程序。最大化_窗口()
#输入要加载的url
驱动程序。获取(“”)
#等待页面加载
驱动程序。隐式等待(5)
#查找上载文件类型并通过测试值
upload\u field=驱动程序。通过部分链接文本(“文件”)查找元素
上传_字段。清除()
上传\字段。发送\密钥(“测试”)
当我运行此代码时,我能够在Chrome浏览器中成功加载页面,但我得到以下异常

# Exception when trying to get element by type
Traceback (most recent call last):
  File "C:\Users\TEST\Desktop\Test.py", line 33, in <module>
    upload_field = driver.find_element_by_partial_link_text('file')
  File "C:\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 453, in find_element_by_partial_link_text
    return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)
  File "C:\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "C:\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"file"}
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.14393 x86_64)
#尝试按类型获取元素时出现异常
回溯(最近一次呼叫最后一次):
文件“C:\Users\TEST\Desktop\TEST.py”,第33行,在
upload\u field=驱动程序。通过部分链接文本(“文件”)查找元素
文件“C:\Python\Python36\lib\site packages\selenium\webdriver\remote\webdriver.py”,第453行,按部分链接查找元素
返回self.find\u元素(by=by.PARTIAL\u LINK\u TEXT,value=LINK\u TEXT)
文件“C:\Python\Python36\lib\site packages\selenium\webdriver\remote\webdriver.py”,第955行,位于find\u元素中
'value':value})['value']
文件“C:\Python\Python36\lib\site packages\selenium\webdriver\remote\webdriver.py”,第312行,执行
self.error\u handler.check\u响应(响应)
文件“C:\Python\Python36\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第237行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“部分链接文本”,“选择器”:“文件”}
(会话信息:chrome=63.0.3239.132)
(驱动程序信息:chromedriver=2.35.528161(5b82f2d2aae0ca24b877009200ced9065a772e73),平台=Windows NT 10.0.14393 x8664)
我查看了提供的解决方案,但这也是一个错误。我目前正在使用Python 3.6.4 x64和Selenium 3.8.1。我的操作系统是Windows7 x64位。如何获取html中带有“type”的元素?

查找元素时签出。我发现XPath或css选择器特别强大,因为它们非常通用

xpath css选择器
通过部分链接查找元素\u文本
查找元素文本。此外,它仅适用于
标记。例如,
driver.find\u element\u by\u partial\u link\u text('file')
将计算以下html

<a type="file" name="filePath">file</a>

在selenium中使用部分文本不是正确的方法。 请通过链接了解如何使用部分链接

回答你的问题。 使用其他属性(如名称)来标识定位器


否则,请尝试此定位器“//input[@name='filePath']”

为什么不尝试完整的xpath(//input[@name='filePath']),而不是部分文本。有点离题,但不要隐式使用
等待
使用。它会更快更可靠。谢谢@Alex。我使用了
驱动程序.find\u element\u by_xpath
方法来查找
文件和
提交两个元素。它工作得完美无缺。我还有一个后续问题。
file
元素期望传递一个文件。如果我想让用户选择他选择的文件,而不是在代码中硬编码,该怎么办?@code_Sipra这应该会弹出一个对话框,用户可以在其中选择一个文件。。。如果你有问题,你可能想问一个新问题!嗯,它似乎没有弹出对话框。但正如您所建议的,我可以使用'tkinter'让用户单独提供上传文件的路径。感谢您的投入和指导。这解决了我在selenium中读取元素的所有问题。@Code_Sipra很高兴听到这个消息!对不起,我还有一个问题。通常,要上传的文件会很大~50-100mb大小。上传文件后,系统不会显示任何“上传完成”消息。如何确保在单击“提交”按钮之前完整上载整个文件?我需要在SO中单独提出这个问题吗?谢谢@Guy,这适用于元素
文件
。它不适用于元素
Submit
@code\u Sipra,因为Submit没有
name
属性。您可以使用
css\u选择器
通过
驱动程序进行定位。通过css\u选择器('[value=“Upload File”]”)查找元素
通过部分链接查找元素
文本()!适当更新您的答案,以免混淆OP@Andersson编辑。感谢@Pradeep hebbar的输入。我使用了
驱动程序。在这种情况下,通过部分链接文本查找元素不正确。感谢您分享有关如何正确使用的链接。这可能对我将来有所帮助。
upload_field = driver.find_element_by_css_selector("input[name='filePath'][type='file']")
<a type="file" name="filePath">file</a>
driver.find_element_by_name('filePath')