Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find element using type in Selenium and Python

I have the below html code.

<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.

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.

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

like image 283
Code_Sipra Avatar asked Jan 21 '18 09:01

Code_Sipra


People also ask

How do I find an element that contains specific text in Selenium Webdriver Python )?

We can find an element that contains specific text with Selenium webdriver in Python using the xpath. This locator has functions that help to verify a specific text contained within an element. The function text() in xpath is used to locate a webelement depending on the text visible on the page.

Can I find element by class Selenium Python?

Luckily, Selenium offers a few methods you can use to find elements. One of the options the Selenium WebDriver library provides for locating HTML elements is the use of the class property. The HTML class property is used for setting a class to an HTML element.

How do I find an element in Python?

Locate Elements by Tagname This method allows you to find a web-element by specifying the tag name. It will return the first element having the specified name. If the search doesn't succeed, the method will throw the NoSuchElementException. The above code has a title tag with some text.


Video Answer


3 Answers

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

xpath

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

css selector

upload_field = driver.find_element_by_css_selector("input[name='filePath'][type='file']")
like image 184
Alex Avatar answered Oct 17 '22 12:10

Alex


find_element_by_partial_link_text looks 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

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

But not your html as the element has no text.

You could locate the element by the name attribute instead

driver.find_element_by_name('filePath')
like image 27
Guy Avatar answered Oct 17 '22 12:10

Guy


Its not the proper way to use the partial text in 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/

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

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

like image 1
Pradeep hebbar Avatar answered Oct 17 '22 11:10

Pradeep hebbar