I've tried for several hours to click a button on webpage with codes below but can't find solution.
I tried to click it with Xpath, full-Xpath, class, but it didn't work.
I heard that 'iframe' can occur error, but I don't see any tags named 'frame'
(Or can the iframe tag be in the document under a different name??)
For your information, when I press the button, it does not direct to a new page, but a pop-up window appears to fill out the contents.
Error mesaage
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"btnReleaseVendorInfoModal"}
(Session info: chrome=98.0.4758.102)
button
<button type="button" class="btn btn-warning btn-sm" id="btnReleaseVendorInfoModal"> 반출기본정보 수정</button>
Python code
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
from selenium.common.exceptions import StaleElementReferenceException
# Login
# put id
driver = webdriver.Chrome()
driver.get("https://po-management.net/release/list")
time.sleep(1)
elem = driver.find_element_by_name("username")
elem.send_keys(usernameStr)
elem.send_keys(Keys.RETURN)
time.sleep(2)
# put password
password = driver.find_element_by_xpath('//*[#id="input73"]')
try:
ActionChains(driver).send_keys(Keys.TAB).send_keys(passwordStr).perform()
password.send_keys(passwordStr)
except StaleElementReferenceException:
pass
password.send_keys(Keys.RETURN)
# redirect
time.sleep(3)
url = "https://po-management.net/release/list"
driver.get(url)
# search
time.sleep(1)
search = driver.find_element_by_id("releaseSeqArray")
search.send_keys(vrorder)
search.send_keys(Keys.RETURN)
# click1
time.sleep(1)
driver.find_element_by_link_text(vrorder).click()
# click2
time.sleep(3)
driver.find_element_by_link_text("btnReleaseVendorInfoModal").click()
To click() on the 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-warning.btn-sm#btnReleaseVendorInfoModal"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-warning btn-sm' and #id='btnReleaseVendorInfoModal']"))).click()
Using XPATH and the innerText:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space(text())='반출기본정보 수정']"))).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
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
Related
I am trying to get data from a password protected website with Selenium.
However I get stuck right in the beginning at login with the following error message:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"}
(Session info: chrome=87.0.4280.66)
The name I use is correct for sure, I inspected the website. Including waiting time did not help either...
My code is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from getpass import getpass
driver = webdriver.Chrome()
driver.get("https://clearing.apcs.at/emwebapcsem/startApp.do")
print(driver.title)
print(driver.current_url)
# create an object for searchbox
username=driver.find_element_by_name("username")
password=driver.find_element_by_name("password")
# typte the input
username.send_keys("XXXXXX")
password.send_keys("XXXXXX")
driver.find_element_by_name('login').click()
Any suggestions would be appreciated.
To send a character sequence to the Benutzer and Passwort field as the elements are within an <frame> 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 either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://clearing.apcs.at/emwebapcsem/startApp.do')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[title='menu']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.loginContentBoxInput[name='username']"))).send_keys("Endre")
driver.find_element_by_css_selector("input.loginContentBoxInput[name='password']").send_keys("Endre")
Using XPATH:
driver.get('https://clearing.apcs.at/emwebapcsem/startApp.do')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[#title='menu']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='loginContentBoxInput' and #name='username']"))).send_keys("Endre")
driver.find_element_by_xpath("//input[#class='loginContentBoxInput' and #name='password']").send_keys("Endre")
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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
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
I try to handle a dropdown menu to click on the 'Popular' option within this website using selenium, but no one example which I found doesn't suit for that.
<select class="select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT">
<option value="desc__" selected="">Highest User Rating</option><option
value="desc__discount_percent">Discount</option><option value="asc__price">Price: Low to High</option><option value="desc__price">Price: High to Low</option><option value="desc__ratings_count">Popular</option></select>
Have used CSS, Xpath and Select, but the result is the same: No such element.
Bellow you can see attempts and outputs.
Any ideas what I do wrong?
CSS Selector
browser.find_element_by_css_selector('.select__select--2gOcq.explorerSortMenu__explorerSortPopoutMenu--3pMwT')
Message: no such element: Unable to locate element: {"method":"css selector","selector":".select__select--2gOcq.explorerSortMenu__explorerSortPopoutMenu--3pMwT"}
Xpath
browser.find_element_by_xpath('//input[starts-with(#class,"select__select--2gOcq")]')
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[starts-with(#class,"select__select--2gOcq")]"}
Select
Select(browser.find_element_by_xpath("//*[#class='select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT']"))
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#class='select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT']"}
UPDATE:
After executed the code bellow the element was successfully located but, I have caught the TimeoutException.
driver = webdriver.Chrome()
driver.get(URL)
try:
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(#class, 'select__select--') and contains(#class, 'explorerSortMenu__explorerSortPopoutMenu--')]"))))
select.select_by_visible_text('Popular')
select.click()
finally:
driver.quit()
As the dropdown is based on <span> and <div> nodes so you can't use Select class and to click on the option Popular with in the website you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.vivino.com/explore?e=eJzLLbI11jNVy83MszU1MFDLTaywNTIAMpIrbT391JKBRJBaga2hWnqabVliUWZqSWKOWm6yrVp-EhDbpqQWJ6uVl0THAlWAKSMAxOAYsg==")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#class, 'responsiveDropdownMenu__title--')]//following::span[starts-with(#class, 'responsiveDropdownMenu__label--')]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#class, 'responsiveDropdownMenu__menu--')]//a[#id='desc__ratings_count']"))).click()
Browser Snapshot:
It would help to have the actual html but you could try using one of the multi-valued classes or even reversing the class order. Examples (these both work when testing on html sample):
.explorerSortMenu__explorerSortPopoutMenu--3pMwT.select__select--2gOcq
or
.select__select--2gOcq
I am trying to click on the "All Topics" and "All States" CheckBoxes then search the results. When I run the script, a chrome window opens up in size 1036x674.
If I leave the window alone, I get element click interception errors. If I minimize or maximize the window, my script works fine.
I am using Selenium 3.141.0, chrome 76, chromedriver 76, and python 3.6
chromedriver_path = r"C:\Users\path\to\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx"
topics_xpath = "//*[#id=\"dnn_ctr81355_StateNetDB_UpdatePanel1\"]/div[1]/div[2]/span/label"
states_xpath = "//*[#id=\"dnn_ctr81355_StateNetDB_UpdatePanel1\"]/div[2]/div[2]/span/label"
browser.get(url)
time.sleep(30)
elem = browser.find_element_by_xpath(topics_xpath)
elem.click()
time.sleep(5)
elem = browser.find_element_by_xpath(states_xpath)
elem.click()
But I get this error:
ElementClickInterceptedException: Message: element click intercepted:
Element <label for="dnn_ctr81355_StateNetDB_ckBxAllTopics">...</label> is not clickable at point (259, 665).
Other element would receive the click:
<label for="dnn_ctr81355_StateNetDB_ckBxTopics_0">...</label>
(Session info: chrome=76.0.3809.100)
The CheckBox that would be clicked is right below the one I am trying to click.
You need WebDriverWait to make sure the element visibility_of_element_located, then scroll to Searchable Database section, and you can use locator by xpath.
Please import :
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Try the bellow code.
chromedriver_path = r"C:\Users\path\to\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx"
topics_xpath = "//div[#class='divTopicsSection1']//span//label[text()='All Topics']"
states_xpath = "//div[#class='divStatesSection1']//span//label[text()='All States']"
dBase_xpath = "//h4[text()='Searchable Database']"
browser.get(url)
WebDriverWait(browser, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, topics_xpath)))
elem = browser.find_element_by_xpath(dBase_xpath)
browser.execute_script("arguments[0].scrollIntoView(true);", elem)
browser.find_element_by_xpath(topics_xpath).click()
browser.find_element_by_xpath(states_xpath).click()
This error message...
ElementClickInterceptedException: Message: element click intercepted
...implies that the click method invoked on the desired element was intercepted by some other element.
To click() on the checkboxes associated with text as All Topics and All States you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for$='_StateNetDB_ckBxAllTopics']"))).click()
driver.find_element_by_css_selector("label[for$='_StateNetDB_ckBxAllStates']").click()
Using XPATH:
driver.get("http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(#for, '_StateNetDB_ckBxAllTopics')]"))).click()
driver.find_element_by_xpath("//label[contains(#for, '_StateNetDB_ckBxAllStates')]").click()
Browser Snapshot:
I have a specific login button I can't access with Python Firefox Selenium. It's the login button on this webpage: https://schalter.asvz.ch/tn/lessons/39616
I'm running Ubuntu 16.04, Python 3.5.2, Firefox 65.0 and Selenium 3.141.
I tried several combinations of approaches I found here on stackoverflow including the following:
login = driver.find_element_by_xpath("//*[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_xpath("//button[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_class_name('btn btn-default ng-star-inserted')
login = driver.find_element_by_xpath("//*[contains(., 'Login')]")
login = driver.find_element_by_name('app-lessons-enrollment-button')
But none of them worked. Always resulting in:
NoSuchElementException: Message: Unable to locate element:
//*[#class='btn btn-default ng-star-inserted']
What is so different with this button? How can I make it work?
This error message...
NoSuchElementException: Message: Unable to locate element: //*[#class='btn btn-default ng-star-inserted']
...implies that the ChromeDriver was unable to locate the desired element through the locator you have used.
Virtually, your first two(2) locators were just perfecto.
However, the desired element is an Angular 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 Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-default.ng-star-inserted[title='Login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-default ng-star-inserted' and #title='Login']"))).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
Try the below option.
Login=driver.find_element_by_css_selector("button.ng-star-inserted")
Or try this
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button.ng-star-inserted'))).click()
You need following imports for option 2.
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
Following xpath works just fine (tested using selenium java)
//button[#title='Login']
I was able to locate and click the button.
Try the below xpath:
xpath = "//button[#title='Login']"
element = driver.find_element_by_xpath(xpath);
element.click();
Unable to interact with href link.
Code trials:
browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get(URL)
webbrowser.open(URL)
#if Size == 'Large':
ClickS =browser.find_element_by_id('product-select').click()
SizeS = browser.find_element_by_xpath("//option[#value='12218866696317']").click()
#Send to cart
AddtoCart = browser.find_element_by_css_selector("input[type='submit']").click()
GotoCart = browser.find_element_by_partial_link_text("Cart").click()
Code and Error snapshot:
HTML:
Cart
HTML Snapshot:
This error message...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element {"method":"link text","selector":"Cart"}
...implies that the ChromeDriver was unable to locate the desired element as per the line:
GotoCart = browser.find_element_by_link_text("Cart").click()
Solution
You need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
Using LINK_TEXT:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Cart"))).click()
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "section#header a.cart-heading[href='/cart']"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[#id='header']//a[#class='cart-heading' and #href='/cart']"))).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
PS: You can find a detailed discussion in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
The error is at the bottom of the stack trace, it can't find the element from the link text that you gave it. It's probably the same problem that this person had where the python was going too fast and the page hadn't fully loaded: How to use find_element_by_link_text() properly to not raise NoSuchElementException?
So just add browser.implicitly_wait(10) right after the line where you set browser.