如何在Selenium和Python中使用类型查找元素 [英] How to find element using type in Selenium and Python

查看:191
本文介绍了如何在Selenium和Python中使用类型查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的html代码.

I have the below html code.

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

我正在尝试使用Selenium和Python查找两个元素文件"和提交".下面是我尝试使用的代码.

I am trying to find the two elements "file" and "submit" using Selenium with Python. Below is the code I have tried to use.

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")

运行此代码时,我可以在Chrome浏览器中成功加载页面,但出现以下异常.

When I run this code, I am able to load the page successfully in the Chrome browser but I get the below exception.

# 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)

我查看了此处提供的解决方案但这也会引发错误.我目前正在将Python 3.6.4 x64与Selenium 3.8.1结合使用.我的操作系统是Windows 7 x64位.如何在html中获取带有类型"的元素?

I looked at the solution provided here but this too is throwing an error. I am currently using Python 3.6.4 x64 with Selenium 3.8.1. My OS is Windows 7 x64 bit. How can I get elements with 'type' in html?

推荐答案

结帐文档查找元素.我发现xpaths或css选择器特别强大,因为它们具有极强的通用性.

Checkout the docs on finding elements. I find xpaths or css selectors particularly powerful because they are extremely generalizable.

upload_field = driver.find_element_by_xpath("//input[@type='file']")

css选择器

upload_field = driver.find_element_by_css_selector("input[name='filePath'][type='file']")

这篇关于如何在Selenium和Python中使用类型查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆