After opening up a webpage, webdriver can't find the "name" element?
Traceback (most recent call last):
File "main.py", line 19, in <module>
driver.find_element(By.NAME,"nickname").send_keys(username+Keys.ENTER)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, 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="nickname"]"}
(Session info: chrome=)
Stacktrace:
#0 0x5574d3a05919 <unknown>
Here's the HTML source I used:
<input name="nickname" type="text" placeholder="Nickname" maxlength="15" id="nickname" data-functional-selector="username-input" class="sc-gTgzIj eFnEAY" autocomplete="off" value="" aria-expanded="false">
Aan <inpt> element is a clickable element and ideally to locate any clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#nickname[name='nickname'][placeholder='Nickname'][data-functional-selector='username-input']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='nickname' and #name='nickname'][#placeholder='Nickname' and #data-functional-selector='username-input']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
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 xpath and see, if your desired element is getting highlighted with 1/1 matching node.
If this is unique //input[#id='nickname' and #name='nickname'] then you need to check for the below conditions as well.
Check if it's in any iframe/frame/frameset.
Check if it's in any shadow-root.
Make sure that the element is rendered properly before interacting with it. Put some hardcoded delay or Explicit wait and try again.
If you have redirected to a new tab/ or new windows and you have not switched to that particular new tab/new window, otherwise you will likely get NoSuchElement exception.
If you have switched to an iframe and the new desired element is not in the same iframe context then first switch to default content and then interact with it.
to switch to frame:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "frame XPath here")))
to switch to default content:
driver.switch_to.default_content()
If you're using explicit wait, then you would need below imports as well.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related
I have been trying to click the "Create an Account" button on this webpage with selenium and python but python cannot seem to find the element. Here is my current code:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen")
time.sleep(12)
accountcreate = driver.find_element_by_class_name ('btn-group btn-group-create-account ng-scope')
accountcreate.click()
Every time I run it, chrome opens to the webpage but it does not click on the button and I get this response:
File "skit.py", line 8, in <module>
link = driver.find_element_by_class_name ('btn-group btn-group-create-account ng-scope')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, 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":".btn-group btn-group-create-account ng-scope"}
(Session info: chrome=83.0.4103.97)
I have tried to use different methods to identify the element such as XPath, css, and more but I still cannot manage to locate it and click it. I believe it has something to do with Iframes but I am not completely sure. Does anyone have any idea on how to solve this?
Thanks!
The link with text as Create an Account is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element_to_be_clickable().
You can use the following Locator Strategies:
LINK_TEXT:
driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"disneyid-iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Create an Account"))).click()
Using CSS_SELECTOR:
driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='disneyid-iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-secondary.ng-isolate-scope"))).click()
Using XPATH:
driver.get("https://www.shopdisney.com/merch-pass/product-selection/arendelle-castle-collection-from-frozen")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='disneyid-iframe']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Create an Account']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
When I am trying to click the Incident tab as highlighted in Image, it gives me an error. Below is the script.
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://*****.service-now.com/")
driver.switch_to.frame('gsft_main')
driver.find_element_by_id('user_name').send_keys('*****')
driver.find_element_by_id('user_password').send_keys('*****')
driver.find_element_by_id('sysverb_login').click()
Incident=driver.find_element_by_link_text('Incident')
This is the element I got when copied after Inspecting the page
<span>Incident</span>
This is xpath
//*[#id="concourse_application_dead1309c611228701e2bda7b4252474"]/a/span
Tried both but no success. Gives me below error.`
Traceback (most recent call last):
File "Test1.py", line 10, in <module>
Incident=driver.find_element_by_link_text('Incident')
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value']
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Incident"}
find_element_by_link_text() and find_element_by_partial_link_text() works only on <a> tags, your element is a <span>. You need to use xpath for to locate it
incident = driver.find_element_by_xpath('//span[contains(., "Incident")]')
Since you are navigated to another page you should also add some wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
incident = wait.until(ec.visibility_of_element_located((By.XPATH, '//span[contains(., "Incident")]')))
The element with text as Incident is actually within a <span> node. Though it is within a parent <a> node, you won't be able to use find_element_by_link_text() or find_element_by_partial_link_text() to locate the element.
However, as the desired element is a dynamic element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
incident = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[id^='concourse_application_dead']>a>span")))
Using XPATH A:
incident = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Incident']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a detailed relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
HTML
I'm trying to write into "Security Code"
EDIT HTML CODE
I tried to write this:
CVXPATH = '//input[#type="tel"]'
cv=driver.find_element_by_xpath(CVXPATH)
cv.send_keys("000")
But I have this error:
line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
How can I solve this?
Problem clicking the button
I tried this method that works perfectly for other buttons (different class) on the same web page but not this specific button, I also tried using div class:
xpathoo = '//button[#class="ncss-brand pt2-sm pr5-sm pb2-sm pl5-sm ncss-btn-accent continueOrderReviewBtn mod-button-width ncss-brand\
pt3-sm prl5-sm pb3-sm pt2-lg pb2-lg d-sm-b d-md-ib u-uppercase u-rounded fs14-sm"]'
driver.find_element_by_xpath(xpathoo).click()
The element is present inside an iframe you need to switch to iframe first in order to send values in input field..
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following CSS selctor.
Induce WebDriverWait() and wait for element_to_be_clickable() and following XPATH
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'iframe.credit-card-iframe-cvv')))
cv=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[#id="cvNumber" and #type="tel"]')))
cv.send_keys("000")
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
I don't know the element used to click the button.
I tried to write like this:
driver.find_element_by_xpath('//*/input[#type="button"]').click()
Error message:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message:
element not visible
HTML:
<input type="button" name="ctl00$c3$g_6_f947_400a_aa18_59efd84584ae$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem" value="Save" onclick="if (!PreSaveItem()) return false;if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl33$g_69_f947_400a_aa18_59efd84584ae$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem", "", true, "", "", false, true))" id="ctl00_ctl33_g_696_f947_400a_aa18_59efd84584ae_ct0_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem" accesskey="O" class="ms-ButtonHeightWidth" target="_self">
Is the 'Save' word visible? if so you can try this:
driver.find_element_by_xpath("//*[contains(text(), 'Save')]").click()
Have you tried looking for the value?
driver.find_element_by_xpath('//*/input[#value="Save"]').click()
If this doesn't work it would be helpful if you could upload the HTML for the page you are testing or provide the URL.
Not sure why you are using //*/input rather using direct //input. Here is the solution.
driver.find_element_by_xpath("//input[#type='button' and #value='Save']").click()
The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ms-ButtonHeightWidth[value='Save'][name$='SaveItem']"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='ms-ButtonHeightWidth' and #value='Save'][contains(#name, 'SaveItem')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Hello
I created few selenium scripts before on python and they are working fine but don't know why it's not working on a website.
I'm trying to click on a element which appear when "DISQUS" is loaded on that website.
Disqus is a comment section on website which loads after website loading.
So I used sleep method but it didn't work so I tried it with python IDLE to execute code line one by one so I can run locate code once Disqus comment section is completely available but still getting same error. "Webdriver unable to locate element"
Here is my code.
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome('E:\\ChromeDriver\\chromedriver_win32\\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
dicuss = browser.find_element_by_class_name('textarea')
print(dicuss)
Error, I'm getting.
Traceback (most recent call last):
File "C:\Users\Acer-573G\PycharmProjects\Appium\venv\import selenium.py", line 12, in <module>
dicuss = browser.find_element_by_class_name('textarea')
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 563, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"textarea"}
(Session info: chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
I attached the screen shot of inspect element (class) from that website.
Inspect element screen shot
I'm new to programming, any help would be appreciated.
There are multiple iframes, so you have to switch to it before you want the interaction.
It's always a good practice if you switch the control of driver to default once you are done.
Code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://www.eloanus.com/customers-review")
wait = WebDriverWait(driver, 20)
visibilty_login = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Log In']")))
action = ActionChains(driver)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='disqusCommentsHolder']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Disqus']")))
driver.execute_script("window.scrollTo(0, 100)")
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Join the discussion…']/following-sibling::div[#class='textarea']"))).send_keys("Hi")
driver.switch_to.default_content()
Hope this helps.
To send a character sequence to the Disqus comment section you need to:
Induce WebDriverWait for the desired parent frame to be available and switch to it
Induce WebDriverWait for the desired nested frame to be available and switch to it
Induce WebDriverWait for the desired element to be clickable and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='disq2disqusCommentsHolder']")))
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id,'dsq-app') and #title='Disqus']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='textarea'][contains(#aria-label,'Join the discussion')]"))).send_keys("Michael Berger")
Browser Snapshot: