I am a new Python programmer. Lately, I started working on automation/scraping. Currently, I am stuck in a situation where a page appears after a button click, and I am not able to locate elements on that page.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome(r"/Users/prateep/Downloads/chromedriver")
driver.get("https://example.com")
driver.maximize_window()
time.sleep(10)
button = driver.find_element_by_class_name("a-IRR-buttons")
button.click()
time.sleep(5)
driver.find_element("P52_NAME").send_keys("Test!!!")
selectStatus = Select(driver.find_element("P52_ASSESSMENT_STATUS_ID"))
selectStatus.select_by_value("O - Open")
driver.close()
Output:-
driver.find_element_by_name("P52_NAME").send_keys("Test!!!")
Traceback (most recent call last):
File "/Users/prateep/Aegis_Automation/Aegis_Automation.py", line 25, in <module>
driver.find_element_by_name("P52_NAME").send_keys("Test!!!")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 658, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="P52_NAME"]"}
CodePudding user response:
Issue is at this line
driver.find_element("P52_NAME").send_keys("Test!!!")
You are using find_element
wrong.
Let's see what do we have in find_element
internally
def find_element(self, by=By.ID, value=None):
"""
Find an element given a By strategy and locator. Prefer the find_element_by_* methods when
possible.
:Usage:
element = driver.find_element(By.ID, 'foo')
:rtype: WebElement
"""
if self.w3c:
if by == By.ID:
by = By.CSS_SELECTOR
value = '[id="%s"]' % value
elif by == By.TAG_NAME:
by = By.CSS_SELECTOR
elif by == By.CLASS_NAME:
by = By.CSS_SELECTOR
value = ".%s" % value
elif by == By.NAME:
by = By.CSS_SELECTOR
value = '[name="%s"]' % value
return self.execute(Command.FIND_ELEMENT, {
'using': by,
'value': value})['value']
As you can see, we have to use it with
By.ID
By.CLASS_NAME
By.TAG_NAME
By.LINK_TEXT
By.PARTIAL_LINK_TEXT
By.NAME
By.CSS_SELECTOR
By.XPATH
If P52_NAME
is the name and if it is unique and represent the desired element in HTMLDOM
, then ideally you should use
driver.find_element(By.NAME, "P52_NAME").send_keys("Test!!!")
Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL F
-> then paste the NAME
and see, if your desired element
is getting highlighted with 1/1
matching node.
Update:
wait = WebDriverWait(driver, 30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "iframe xpath")))
wait.until(EC.visibility_of_element_located((By.ID, ""))).send_keys("")
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC