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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48365252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:40:32  来源:igfitidea点击:

How to find element using type in Selenium and Python

pythonpython-3.xseleniumselenium-webdriverselenium-chromedriver

提问by Code_Sipra

I have the below html code.

我有以下 html 代码。

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

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

我正在尝试使用 Selenium 和 Python 找到“文件”和“提交”这两个元素。下面是我尝试使用的代码。

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

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

当我运行此代码时,我能够在 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)

I looked at the solution provided herebut 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?

我查看了此处提供的解决方案,但这也引发了错误。我目前正在使用 Python 3.6.4 x64 和 Selenium 3.8.1。我的操作系统是 Windows 7 x64 位。如何在 html 中获取带有“type”的元素?

回答by Alex

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

查看有关查找元素的文档。我发现 xpaths 或 css 选择器特别强大,因为它们非常通用。

xpath

路径

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

css selector

css 选择器

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

回答by Guy

find_element_by_partial_link_textlooks for the element text. In addition, it works only on <a>tags. For example, driver.find_element_by_partial_link_text('file')will math the following html

find_element_by_partial_link_text查找元素text。此外,它仅适用于<a>标签。例如,driver.find_element_by_partial_link_text('file')将对以下html进行数学运算

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

But not your html as the element has no text.

但不是您的 html,因为该元素没有文本。

You could locate the element by the nameattribute instead

您可以通过name属性来定位元素

driver.find_element_by_name('filePath')

回答by Pradeep hebbar

Its not the proper way to use the partial textin selenium . Please go through the link to understand how to use partial link https://www.softwaretestingmaterial.com/how-to-locate-element-by-link-text-and-partial-link-text-locator/

它不是使用partial textin selenium的正确方法。请通过链接了解如何使用部分链接https://www.softwaretestingmaterial.com/how-to-locate-element-by-link-text-and-partial-link-text-locator/

Answer to your question. Use the other attribute like name to identify the locator.

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

Otherwise try this locator "//input[@name='filePath']"

否则试试这个定位器“//input[@name='filePath']”