selenium python can't detect id nor name
I tried to access a website whit selenium but python cant find the id nor the name. Here is the code:
from selenium import webdriver from selenium.webdriver.common.keys import Keys browser = webdriver.Firefox(executable_path='D:\code\geckodriver.exe') browser.get('http://admin.chhs.edu.my/schooladmin/jw/login.php') assert "教务系统" in browser.title def login(): elem = browser.find_element_by_name('txtUserName') elem.send_keys('sportday') elem = browser.find_element_by_name('txtPassWord') elem.send_keys('xxxxxx') elem.send_keys(Keys.RETURN) def search(): elem2 = browser.find_element_by_name('txtFind') elem2.send_keys('14066') elem2.send_keys(Keys.RETURN) login() search()
And the html of the website I am accessing:
<form name="search" method="post" action="http://admin.chhs.edu.my/schooladmin/jw/index.php"> <input name="txtFind" id="txtFind" type="text"> <input name="button" id="button" value="搜寻" type="submit"> </form>
NOTE the password isn't correct due to some privacy problems.
BTW this thread have been solved, thank you everyone
Replace this code :
def search(): elem2 = browser.find_element_by_name('txtFind') elem2.send_keys('14066') elem2.send_keys(Keys.RETURN)
To this :
def search(): elem2 = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, "txtFind"))) elem2.send_keys('14066') elem2.send_keys(Keys.RETURN)
Note that if ID is given , then there is no need to go for any locator.
2. Getting Started, The above script can be saved into a file (eg:- python_org_search.py ), then it WebDriver will wait until the page has fully loaded (that is, the “onload” event The easiest way to install Selenium on a Python environment is through the installer pip. pip install selenium. While the installation of Selenium makes the functionality available to you, you need additional drivers for it to be able to interface with a chosen web browser. The download links for the drivers are available here: Chrome, Edge
You've only posted some of the html. One option is that the locators for the login aren't valid. The other option is that the program begins looking for the elements before the page is loaded. In this case the exception would state that the element was not found.
I had a similar issue and what worked for me was to place an
explicit wait on the first element I would be searching for in the new page. An explicit wait waits a specified amount of time for a specified condition to be met.
element = WebDriverWait(driver, seconds).until(EC.presence_of_element_located(By.NAME, 'txtuserame'))
Alternatively you can use an
implicit wait which ideally causes the driver to wait a specified amount of time if the page hasn't loaded. In my experience it's very dependent on the website and it often doesn't work as intended.
An example of an implicit wait in python would be
Implicit wait relates to the driver rather than the element, and remain in effect for the lifespan of the driver instance.
1. Installation, Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. Selenium Python bindings provide a convenient API to Selenium is mostly used for writing test cases. The selenium package itself doesn’t provide a testing tool/framework. You can write test cases using Python’s unittest module. The other options for a tool/framework are py.test and nose. In this chapter, we use unittest as the framework of choice. Here is the modified example which uses
Selenium with Python, Today you will use a full-fledged browser running in headless mode to do the HTTP requests for you. A headless browser is just a regular web browser, except that pip install -U selenium And use this module in your code . from selenium import webdriver You can also use many of the following as required . from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException Here is an updated answer
Modern Web Automation With Python and Selenium – Real Python, You can then decide if the response of the browser is in line with what you expect. This post assumes that you do not have prior knowledge of By using Python with Selenium, you can launch the browser of your choice, fetch any webpage, programmatically click links, fill out web forms, and use other browser functions like back, forward, and reload. Selenium is very powerful and facilitates more flexibility than web scraping tools like Beautiful Soup and Scrapy.
Selenium Python Bindings, Installing. If you have pip on your system, you can simply install or upgrade the Python bindings: pip install -U selenium. Alternately, you can download the Here are a few quick ideas that would leverage the mild superpower that is Python + Selenium: You could extend the BandLeader class to navigate to album pages and play the tracks you find there. You might decide to create playlists based on your favorite or most frequently heard tracks.
Proboscis, Selenium WebDriver is the one that can automate all these tasks. It can interact with all types of Web browsers available till date like Firefox, Internet Explorer, Python language bindings for Selenium WebDriver. The selenium package is used to automate web browser interaction from Python.