Why am I getting NoSuchElementException for find_element_by_id? - python

When trying to execute the following code:
driver = webdriver.Firefox()
driver.get("https://www.flashscore.co.uk/tennis/")
cookie_button = driver.find_element_by_id("onetrust-accept-btn-handler")
cookie_button.click()
I'm getting this exception:
Exception has occurred: NoSuchElementException
Message: Unable to locate element: [id="onetrust-accept-btn-handler"]
When I look through the HTML on the page I can see the following line which is the button I want to click on:
<button id="onetrust-accept-btn-handler">I Accept</button>
Picture of site below:
Picture of the HTML "Inspector":
(apologies not sure how to copy this as text)
Why can't Selenium locate the element?

You need to use ExplicitWait
The below code works for me :
executablePath = r'C:\geckodriver.exe'
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(executable_path = executablePath, options=options)
driver.maximize_window()
driver.get("https://www.flashscore.co.uk/tennis/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#id='onetrust-accept-btn-handler']"))).click()
print('Operation successful')
Imports :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Related

using selenium with python to google href " Getting this error NoSuchElementException: Message: no such element: Unable to locate element: "

using selenium with python to google href or click on the links to open them but Getting this error NoSuchElementException: Message: no such element: Unable to locate element: "
This is the Code I wrote to do it.
url = "https://www.google.com/search?q="+text_fetch # The Dutch Dress in Orange—Why?
driver.get(url)
time.sleep(7)
Ggl_results = driver.find_element(By.XPATH, '//*[#id="rso"]/div[1]/div/div/div[1]/div/a') # finds webresults
time.sleep(7)
for result in Ggl_results:
print(result.get_attribute("href"))
Instead of '//*[#id="rso"]/div[1]/div/div/div[1]/div/a' this XPath should work: //*[#id="rso"]//a[#data-ved][#ping]. I tested that on other random inputs and it worked.
Also, instead of hardcoded sleeps time.sleep(7) WebDriverWait expected_conditions explicit waits should be used.
This coded worked:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.google.com/search?q=The Dutch Dress in Orange—Why?"
driver.get(url)
results = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//*[#id='rso']//a[#data-ved][#ping]")))
for result in results:
print(result.get_attribute("href"))
This is the output:
https://www.dutchamsterdam.nl/321-why-the-dutch-wear-orange
https://netherlandsinsiders.com/why-is-the-national-color-of-the-netherlands-orange/
https://www.distractify.com/p/why-do-netherlands-wear-orange
https://dutchreview.com/culture/history/why-do-the-netherlands-love-orange-the-full-explainer/
https://www.quora.com/Why-do-the-Dutch-wear-orange-when-their-flag-doesnt-have-orange-in-it
https://aboutthenetherlands.com/the-reason-why-dutch-people-like-the-orange-color/
https://www.iamexpat.nl/lifestyle/lifestyle-news/most-googled-why-do-dutch-wear-orange
https://taiwanholland.com/holland-a-z/how-to-dress-up-orange-like-the-dutch/
https://amsterdamhangout.com/why-is-orange-the-national-colour-of-the-netherlands/

"Unable to locate element" or "TimeoutException" errors when trying to click on a button with selenium

I want to click on "Show more" under the description of this listing by using selenium (I am trying to collect data of multiple listings).
This is what the HTML looks like
<button type="button" class="b1k5q1b3 v19vkvko dir dir-ltr">...</button>
I have tried using By.CLASS_NAME method but I get an error saying unable to locate element:
url = "https://www.airbnb.com/rooms/50293998?adults=1&children=0&infants=0&check_in=2022-06-21&check_out=2022-06-28&federated_search_id=9f8562f3-653a-45b7-b6bd-218379131b41&source_impression_id=p3_1653519105_nsyk%2Fkxp2MNH1yam"
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver_path = "C:/Users/parkj/Downloads/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(service = Service(driver_path))
driver.get(url)
time.sleep(5)
button = driver.find_element(By.CLASS_NAME, "b1k5q1b3 v19vkvko dir dir-ltr")
button.click()
This is what the error looks like
If I use By.CSS_SELECTOR, I get a TimeoutException error:
driver_path = "C:/Users/parkj/Downloads/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(service = Service(driver_path))
driver.get(url)
time.sleep(5)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "b1k5q1b3 v19vkvko dir dir-ltr")))
I still get a Timeout error even if I make the driver wait longer.
from typing import Union
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.remote.webdriver import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, WebDriverException
def get_element(driver, by: By, identifier: str, timeout: int) -> Union[WebElement, None]:
try:
return WebDriverWait(driver, timeout).until(
EC.visibility_of_element_located((by, identifier))
)
except (WebDriverException, NoSuchElementException):
return None
driver = WebDriver(service = Service("path_to_driver"))
# The way to do it using class_name
button = get_element(By.CLASS_NAME, "b1k5q1b3 v19vkvko dir dir-ltr", 180)
# If you want to select by css
button = get_element(By.CSS_SELECTOR, ".b1k5q1b3 .v19vkvko .dir .dir-ltr", 180)
Personally I always use this function when working with Selenium. When selecting by classes you have to use By.CLASS_NAME, if you want to use CSS_SELCTOR you must put a . in front of the class names.
TLDR: you are missing a .

Python, selenium - why do i get this error?

I want to access google maps with python, but at first you have to click accept cookies button and I don't know why but I keep getting this error:
File "file", line 12, in
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#id="introAgreeButton"]'))).click() File
"C:\Users\gassp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\support\wait.py",
line 80, in until
raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('PATH')
url = 'https://www.google.com/maps/'
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()
According to https://selenium-python.readthedocs.io/api.html :
exception selenium.common.exceptions.TimeoutException(msg=None, screen=None, stacktrace=None)
Bases: selenium.common.exceptions.WebDriverException
Thrown when a command does not complete in enough time.
Hence you either need to increase the time in WebDriverWait(driver, 10) or check if the condition EC.element_to_be_clickable is even fulfillable.
Furthermore, try the following implementation:
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]')))
element.click()
That fixed a few problems at my end with a similar code structure.
When I tried to access the site I did not found the xpath locator //*[#id="introAgreeButton"] in chrome dev tool. Not sure what element it refers to.
However when I commented that line and added the click event on the search button, I was able to get the results.
Also there is no submit() method available on driver instance.
Below was my trial using the code you have provided -
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='searchbox-searchbutton']"))).click()
Output :-
The problem was that it didn't change between the main frame and the accept cookie frame. Here is the soulution code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('C:\\Users\\gassp\\OneDrive\\Namizje\\Python.projects\\chromedriver.exe')
url = 'https://www.google.com/maps/'
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[#id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()

I keep getting the error message NoSuchElementException when trying to use selenium to log into my university's webpage

I'm pretty new to python and StackOverflow so please bear with me.
I'm trying to write a script in python and use selenium to log myself into my university's website but I keep getting the same error NoSuchElementException.
The full text of the error:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="username"]"}
(Session info: chrome=86.0.4240.183)
File "C:\Users\User\Desktop\Python\Assignment6\nsuokSelenium.py", line 9, in <module>
browser.find_element_by_id('username').send_keys(bb_username)
I have my log in information in a separate script called credential.py that I'm calling with
from credentials import bb_username, bb_password
My Code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from credentials import bb_password, bb_username
browser = webdriver.Chrome()
browser.get('https://bb.nsuok.edu')
browser.find_element_by_id('username').send_keys(bb_username)
browser.find_element_by_id('password').send_keys(bb_password)
browser.find_element_by_name('submit').click()
try:
WebDriverWait(browser, 1) .until(EC.url_matches('https://bb.nsuok.edu/ultra'))
except TimeoutError:
print('took too long')
WebDriverWait(browser, 10).until(EC.url_matches('https://bb.nsuok.edu/ultra'))
browser.find_element_by_name('Courses').click()
WebDriverWait(browser, 10).until(EC.url_matches('https://bb.nsuok.edu/ultra/course'))
browser.find_element_by_name('Organizations').click()
WebDriverWait(browser, 10).until(EC.url_matches('https://bb.nsuok.edu/ultra/logout'))
The error is showing up here
browser.find_element_by_id('username').send_keys(bb_username)
Could it be an issue with PATH?
What Justin Ezequiel said is correct. You need to add waits in your code for the page to load properly; due to the fact that, dependent upon internet speeds, some pages load faster than others. ( obviously )
With that in mind, I was able to identify the elements on the page for you. I added some comments in the code as well.
MAIN PROGRAM - For Reference
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver as ChromeDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as DriverWait
from selenium.webdriver.support import expected_conditions as DriverConditions
from selenium.common.exceptions import WebDriverException
def get_chrome_driver():
"""This sets up our Chrome Driver and returns it as an object"""
path_to_chrome = "F:\Selenium_Drivers\Windows_Chrome85_Driver\chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
# Browser is displayed in a custom window size
chrome_options.add_argument("window-size=1500,1000")
return webdriver.Chrome(executable_path = path_to_chrome,
options = chrome_options)
def wait_displayed(driver : ChromeDriver, xpath: str, int = 5):
try:
DriverWait(driver, int).until(
DriverConditions.presence_of_element_located(locator = (By.XPATH, xpath))
)
except:
raise WebDriverException(f'Timeout: Failed to find {xpath}')
def enter_information(driver : ChromeDriver, xpath: str, text : str):
driver.find_element(By.XPATH, xpath).send_keys(text)
if(driver.find_element(By.XPATH, xpath).get_attribute('value').__len__() != text.__len__()):
raise Exception(f'Failed to populate our Textbox.\nXPATH: {xpath}')
# Gets our chrome driver and opens our site
chrome_driver = get_chrome_driver()
chrome_driver.get("https://logon.nsuok.edu/cas/login")
# Waits until our elements are loaded onto the page
wait_displayed(chrome_driver, "//form//input[#id='username']")
wait_displayed(chrome_driver, "//form//input[#id='password']")
wait_displayed(chrome_driver, "//form//input[contains(#class, 'btn-submit')]")
# Inputs our Username and Password
enter_information(chrome_driver, "//form//input[#id='username']", "MyUserNameHere")
enter_information(chrome_driver, "//form//input[#id='password']", "MyPasswordHere")
# Clicks Login
chrome_driver.find_element(By.XPATH, "//form//input[contains(#class, 'btn-submit')]").click()
chrome_driver.quit()
chrome_driver.service.stop()
You may need to wait for the element. Try something like the following:
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
element.clear()
element.send_keys(bb_username)
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "password"))
)
element.clear()
element.send_keys(bb_password)

AttributeError("move_to requires a WebElement") AttributeError: move_to requires a WebElement error using Action Class in Selenium Webdriver

Link to website # https://www.2dehands.be/a/zakelijke-goederen/landbouw-veevoer/m1527886462-kleine-pakken-hooi.html?c=c41b759082ce0aa7411a74e54f8dbd13&previousPage=home
I want click this button red in this image:
.
Here is the code:
act = ActionChains(driver)
add_button = driver.find_elements_by_xpath("//button[#class='mp-Button mp-Button--secondary']")
subDiv = driver.find_elements_by_xpath("//div[#class='phone-number-container']")
sleep(5)
actions = ActionChains(driver)
actions.move_to_element(add_button)
actions.click(subDiv)
actions.perform()
But got this error:
raise AttributeError("move_to requires a WebElement")
AttributeError: move_to requires a WebElement
Please find below working solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
#utilise chrome driver to open specified webpage
driver = webdriver.Chrome(executable_path=r"\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.2dehands.be/a/zakelijke-goederen/landbouw-veevoer/m1527886462-kleine-pakken-hooi.html?c=c41b759082ce0aa7411a74e54f8dbd13&previousPage=home")
actionChains = ActionChains(driver)
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, ".contact-options-mobile:nth-child(2) span:nth-child(2)")))
actionChains.move_to_element(element).click().perform()
element1 = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//span[#class='phone-number-bubble']")))
print element1.text
output : 0494383160
or you could have just given find_element_by_xpath instead of find_elements_by_xpath

Categories

Resources