I'm trying to click the play button on amazon photos using selenium in a python script. I've tried using a xpath
play_button_displayed = driver.find_element(By.XPATH,'//*[#id="photos"]/section/section/section/div/div/figure[1]/div/div[1]/figure/figure/button')
play_button_display.click()
i've tried other methods and the most promising in my eyes was the css selector
`
play_button = driver.find_element(By.CSS_SELECTOR,".play")
play_button.click()
Traceback (most recent call last):
File "d:\pictures_backup\tool_used\python\amazon_photos_web_list_creation\amazon_photos_open_and_replace_date_auto_and_manual\amazon_photos_open_and_replace_date_auto_and_manual.py", line 104, in <module>
LaunchBrowser_main()
File "d:\pictures_backup\tool_used\python\amazon_photos_web_list_creation\amazon_photos_open_and_replace_date_auto_and_manual\amazon_photos_open_and_replace_date_auto_and_manual.py", line 53, in LaunchBrowser_main
click_info()
File "d:\pictures_backup\tool_used\python\amazon_photos_web_list_creation\amazon_photos_open_and_replace_date_auto_and_manual\amazon_photos_open_and_replace_date_auto_and_manual.py", line 92, in click_info
play_button = driver.find_element(By.CSS_SELECTOR,"play")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Eric\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 830, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Eric\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\Eric\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, 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":"play"}
(Session info: chrome=110.0.5481.78)
Stacktrace:
Backtrace:
(No symbol) [0x007C37D3]
(No symbol) [0x00758B81]
(No symbol) [0x0065B36D]
(No symbol) [0x0068D382]
(No symbol) [0x0068D4BB]
(No symbol) [0x006C3302]
(No symbol) [0x006AB464]
(No symbol) [0x006C1215]
(No symbol) [0x006AB216]
(No symbol) [0x00680D97]
(No symbol) [0x0068253D]
GetHandleVerifier [0x00A3ABF2+2510930]
GetHandleVerifier [0x00A68EC1+2700065]
GetHandleVerifier [0x00A6C86C+2714828]
GetHandleVerifier [0x00873480+645344]
(No symbol) [0x00760FD2]
(No symbol) [0x00766C68]
(No symbol) [0x00766D4B]
(No symbol) [0x00770D6B]
BaseThreadInitThunk [0x761E00F9+25]
RtlGetAppContainerNamedObjectPath [0x77607BBE+286]
RtlGetAppContainerNamedObjectPath [0x77607B8E+238]
[Done] exited with code=1 in 32.94 seconds
but that didn't work either. i would like to preferable work in css selector since that seems the easiest. any help will be much appreciated.
here's the xpath of buttom from chrome
//*[#id="photos"]/section/section/section/div/div/figure[1]/div/div[1]/figure/figure/button
full xpath
/html/body/div[1]/div/div[2]/section/section/section/div/div/figure[1]/div/div[1]/figure/figure/button
and element
<button class="play"></button>
i've tried, xpath, css selector, class name id ect and they all didn't work but css selector seems the easiest and promising but thats not working lmao
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, "#photos button.play"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='photos']/section/section/section/div/div/figure[1]/div/div[1]/figure/figure/button[#class='play']"))).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
Related
Example
This is a script that should be able to connect to the browser that was launched with debug options in the command prompt. As I provide later I'm no longer able to connect to the remote browser at 127.0.0.1:9222." I've tried searching for a solution, but I haven't found one that works.
Launching browser in with debug port
"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222
Python script
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
driver = webdriver.Edge(options=edge_options)
driver.get(youtube.com)
printf(driver.title)
Problem
For some reason, my code can no longer connect to the browser launched by the command mentioned. This is an error message Im facing.
part of the error message
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222 **from chrome not reachable **
full error message
Traceback (most recent call last):
File "C:\Users\user\OneDrive\Plocha\záložky\msedge - running\Try 18.py", line 7, in <module>
driver = webdriver.Edge(options=edge_options)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\edge\webdriver.py", line 73, in __init__
super().__init__(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chromium\webdriver.py", line 104, in __init__
super().__init__(
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222
from chrome not reachable
Stacktrace:
Backtrace:
Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72EE616C2+15186]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EDFA212+827554]
(No symbol) [0x00007FF72EABEBF3]
(No symbol) [0x00007FF72EAAEC8A]
(No symbol) [0x00007FF72EAF013D]
(No symbol) [0x00007FF72EAE5D4A]
(No symbol) [0x00007FF72EB29C56]
(No symbol) [0x00007FF72EB21D23]
(No symbol) [0x00007FF72EAF3B80]
(No symbol) [0x00007FF72EAF2B0E]
(No symbol) [0x00007FF72EAF4344]
Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72ECDC3B0+182752]
(No symbol) [0x00007FF72EBB0095]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72ED3A6EA+42362]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72ED3D425+53941]
Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF72F058AB3+1456595]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE0276A+861690]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE07854+882404]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE079AC+882748]
Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72EE1097E+919566]
BaseThreadInitThunk [0x00007FF8D3C726BD+29]
RtlUserThreadStart [0x00007FF8D5A6DFB8+40]
What have I tried?
There are many tutorials on using python and chrome, but not many are for an edge. Here are some samples of things I've tried:
https://www.youtube.com/watch?v=Zrx8FSEo9lk
https://www.youtube.com/watch?v=FGaU0OgtbNk&t=181s
https://www.youtube.com/watch?v=pZdMsLKAjs4&t=271s
https://bugs.chromium.org/p/chromedriver/issues/detail?id=710#c3
https://www.geeksforgeeks.org/automated-browser-testing-with-edge-and-selenium-in-python/
https://stackoverflow.com/questions/8344776/can-selenium-interact-with-an-existing-browser-session
Be patient, thanks.
I'm trying to automate comments on an Instagram post using Python and Selenium. I am trying to access the text area where you write comments in all possible ways, such as CLASS_NAME, TAG_NAME, XPATH, CSS_SELECTOR. However, none of these methods are working. I have also tried using the implicit wait method to make sure the element is loaded, but that doesn't work either. I'm not sure what the problem is. I just need someone to tell me how to access the element where comments are written on an instagram post, send a send_keys to that element and hit the submit button.
The current status of the code is this:
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
import os
import pickle
from selenium import __version__
data = {
'username' : 'wendyturner329ysu',
'password' : 'MpEgPAhd',
'url_publication' : 'https://www.instagram.com/p/ClZj_e4vZCF/',
'comment' : 'This is an automated comment!',
'time' : 3
}
def Comments():
driver = webdriver.Chrome()
driver.get("https://www.instagram.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
if cookie['domain'] == ".instagram.com":
try:
driver.add_cookie(cookie)
except Exception as e:
print(e)
driver.get("https://www.instagram.com/p/ClZj_e4vZCF/")
time.sleep(4)
driver.find_element(By.XPATH, '//textarea[#aria-label="Add a comment..."]').send_keys(data['comment'])
time.sleep(5)
Comments()
The error it throws is:
C:\Users\54225\AppData\Local\Programs\Python\Python310\python.exe C:/Users/54225/Documentos/Proyecto/Comentarios_Instagram_Selenium.py
Traceback (most recent call last):
File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 39, in <module>
Comentarios()
File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 36, in Comentarios
driver.find_element(By.XPATH, '//textarea[#aria-label="Agrega un comentario..."]').send_keys(datos['comentario'])
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 233, in send_keys
self._execute(
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 410, in _execute
return self._parent.execute(command, params)
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
self.error_handler.check_response(response)
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=109.0.5414.75)
Stacktrace:
Backtrace:
(No symbol) [0x00F36643]
(No symbol) [0x00ECBE21]
(No symbol) [0x00DCDA9D]
(No symbol) [0x00DD09E4]
(No symbol) [0x00DD08AD]
(No symbol) [0x00DD0B30]
(No symbol) [0x00DFC474]
(No symbol) [0x00DFB7AB]
(No symbol) [0x00E1FD7C]
(No symbol) [0x00DF641F]
(No symbol) [0x00E200D4]
(No symbol) [0x00E36B09]
(No symbol) [0x00E1FB76]
(No symbol) [0x00DF49C1]
(No symbol) [0x00DF5E5D]
GetHandleVerifier [0x011AA142+2497106]
GetHandleVerifier [0x011D85D3+2686691]
GetHandleVerifier [0x011DBB9C+2700460]
GetHandleVerifier [0x00FE3B10+635936]
(No symbol) [0x00ED4A1F]
(No symbol) [0x00EDA418]
(No symbol) [0x00EDA505]
(No symbol) [0x00EE508B]
BaseThreadInitThunk [0x7612FEF9+25]
RtlGetAppContainerNamedObjectPath [0x770A7BBE+286]
RtlGetAppContainerNamedObjectPath [0x770A7B8E+238]
Process finished with exit code 1
When I use implicity wait this happens, this is the code:
datos = {
'usuario' : 'wendyturner329ysu',
'contraseña' : 'MpEgPAhd',
'url_publicacion' : 'https://www.instagram.com/p/ClZj_e4vZCF/',
'comentario' : 'Este es un comentario automatizado!',
'tiempo' : 3
}
def Comentarios():
"""Lo que esta comentado es para usar un proxy especifico"""
#proxy = "136.228.220.49:8082"
#options = webdriver.ChromeOptions()
#options.add_argument('--proxy-server=%s' % proxy)
#driver = webdriver.Chrome(chrome_options=options)
driver = webdriver.Chrome()
driver.get("https://www.instagram.com")
cookies = pickle.load(open("cookies.pkl","rb"))
for cookie in cookies:
if cookie['domain'] == ".instagram.com":
try:
driver.add_cookie(cookie)
except Exception as e:
print(e)
driver.get("https://www.instagram.com/p/ClZj_e4vZCF/")
time.sleep(4)
wait = WebDriverWait(driver, 10)
comment_box = wait.until(EC.presence_of_element_located((By.XPATH, '//textarea[#aria-label="Agrega un comentario..."]')))
comment_box.send_keys(datos['comentario'])
And give me this error:
Traceback (most recent call last):
File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 42, in <module>
Comentarios()
File "C:\Users\54225\Documentos\Proyecto\Comentarios_Instagram_Selenium.py", line 38, in Comentarios
comment_box.send_keys(datos['comentario'])
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 233, in send_keys
self._execute(
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 410, in _execute
return self._parent.execute(command, params)
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
self.error_handler.check_response(response)
File "C:\Users\54225\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=109.0.5414.75)
Stacktrace:
Backtrace:
(No symbol) [0x00F36643]
(No symbol) [0x00ECBE21]
(No symbol) [0x00DCDA9D]
(No symbol) [0x00DD09E4]
(No symbol) [0x00DD08AD]
(No symbol) [0x00DD0B30]
(No symbol) [0x00DFC474]
(No symbol) [0x00DFB7AB]
(No symbol) [0x00E1FD7C]
(No symbol) [0x00DF641F]
(No symbol) [0x00E200D4]
(No symbol) [0x00E36B09]
(No symbol) [0x00E1FB76]
(No symbol) [0x00DF49C1]
(No symbol) [0x00DF5E5D]
GetHandleVerifier [0x011AA142+2497106]
GetHandleVerifier [0x011D85D3+2686691]
GetHandleVerifier [0x011DBB9C+2700460]
GetHandleVerifier [0x00FE3B10+635936]
(No symbol) [0x00ED4A1F]
(No symbol) [0x00EDA418]
(No symbol) [0x00EDA505]
(No symbol) [0x00EE508B]
BaseThreadInitThunk [0x7612FEF9+25]
RtlGetAppContainerNamedObjectPath [0x770A7BBE+286]
RtlGetAppContainerNamedObjectPath [0x770A7B8E+238]
As I can see in your code, you have import for WebDriverWait but you are not using it. You can try below solution.
wait = WebDriverWait(driver, 10)
comment_box = wait.until(EC.presence_of_element_located((By.XPATH, '//textarea[#aria-label="Add a comment..."]')))
comment_box.send_keys(data['comment'])
OR
comment_box = driver.execute_script("return document.querySelector('textarea[aria-label=\"Add a comment...\"]');")
comment_box.send_keys(data['comment'])
I was trying to interact with this website using Selenium in python. I wrote this code to select the radio button using XPATH. But some weird error is showing in my terminal. Can anyone please solve this problem? I tried but can't figure out the problem.
My code.
from select import select
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
import csv
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://www2.illinois.gov/idoc/Offender/Pages/InmateSearch.aspx')
button = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table[2]/tbody/tr/td[1]/table/tbody/tr/td/form/table/tbody/tr/td/input[2]')
button.click()
driver.implicitly_wait(10)
driver.quit()
Error :
DevTools listening on ws://127.0.0.1:62348/devtools/browser/ce37da62-856d-4159-ad45-9eca8e63115a
E:\Fiverr job\Orders\1\test.py:18: DeprecationWarning: find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value=xpath) instead
button = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table[2]/tbody/tr/td[1]/table/tbody/tr/td/form/table/tbody/tr/td/input[2]')
Traceback (most recent call last):
File "E:\Fiverr job\Orders\1\test.py", line 18, in <module>
button = driver.find_element_by_xpath('/html/body/table/tbody/tr/td/table[2]/tbody/tr/td[1]/table/tbody/tr/td/form/table/tbody/tr/td/input[2]')
File "E:\Fiverr job\Orders\1\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 526, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "E:\Fiverr job\Orders\1\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1251, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "E:\Fiverr job\Orders\1\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "E:\Fiverr job\Orders\1\env\lib\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":"xpath","selector":"/html/body/table/tbody/tr/td/table[2]/tbody/tr/td[1]/table/tbody/tr/td/form/table/tbody/tr/td/input[2]"}
(Session info: chrome=102.0.5005.63)
Stacktrace:
Backtrace:
Ordinal0 [0x0054D953+2414931]
Ordinal0 [0x004DF5E1+1963489]
Ordinal0 [0x003CC6B8+837304]
Ordinal0 [0x003F9500+1021184]
Ordinal0 [0x003F979B+1021851]
Ordinal0 [0x00426502+1205506]
Ordinal0 [0x004144E4+1131748]
Ordinal0 [0x00424812+1198098]
Ordinal0 [0x004142B6+1131190]
Ordinal0 [0x003EE860+976992]
Ordinal0 [0x003EF756+980822]
GetHandleVerifier [0x007BCC62+2510274]
GetHandleVerifier [0x007AF760+2455744]
GetHandleVerifier [0x005DEABA+551962]
GetHandleVerifier [0x005DD916+547446]
Ordinal0 [0x004E5F3B+1990459]
Ordinal0 [0x004EA898+2009240]
Ordinal0 [0x004EA985+2009477]
Ordinal0 [0x004F3AD1+2046673]
BaseThreadInitThunk [0x7648FA29+25]
RtlGetAppContainerNamedObjectPath [0x77BE7A7E+286]
RtlGetAppContainerNamedObjectPath [0x77BE7A4E+238]
This is because the element you try to click is located into an iframe:
So you must first switch to it before finding and clicking the desired button:
import time
driver = webdriver.Chrome(options=options, desired_capabilities=capabilities)
driver.get('https://www2.illinois.gov/idoc/Offender/Pages/InmateSearch.aspx')
#wait a little to be sure the iframe is loaded
time.sleep(2)
#find and switch to the iframe
iframe = driver.find_element(By.XPATH, '//*[#id="soi-iframe"]')
driver.switch_to.frame(iframe)
button = driver.find_element(By.XPATH, '/html/body/table/tbody/tr/td/table[2]/tbody/tr/td[1]/table/tbody/tr/td/form/table/tbody/tr/td/input[2]')
button.click()
driver.quit()
Proof of work: (click on pic to zoom in)
I am new to selenium and written a script to login, and when I am running the script getting the following error:-
Error:-
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="SignIn Form_email"]"}
Script:-
url = "https://someurl.com"
driver = webdriver.Chrome(executable_path="C:\CustomDrivers\chromedriver_win32\chromedriver")
driver.get(url)
print(driver.title)
user_id = driver.find_element_by_id("SignIn Form_email")
user_id.send_keys("admin#web.com")
user_password = driver.find_element_by_id("SignIn Form_password")
user_password.send_keys("web$$DD")
driver.find_element_by_xpath("//*[#id='SignIn Form']/div[3]/div/div/div/button").click()
print(driver.title)
time.sleep(5)
Full Error Trace:-
C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path="C:\CustomDrivers\chromedriver_win32\chromedriver")
Convin
C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py:9: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
user_id = driver.find_element_by_id("SignIn Form_email")
Traceback (most recent call last):
File "C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py", line 9, in <module>
user_id = driver.find_element_by_id("SignIn Form_email")
File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 466, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\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":"[id="SignIn Form_email"]"}
(Session info: chrome=95.0.4638.69)
Stacktrace:
Backtrace:
Ordinal0 [0x00780C43+2493507]
Ordinal0 [0x0071A4B1+2073777]
Ordinal0 [0x00622608+1058312]
Ordinal0 [0x0064CAA4+1231524]
Ordinal0 [0x00676C62+1404002]
Ordinal0 [0x0066597A+1333626]
Ordinal0 [0x00675038+1396792]
Ordinal0 [0x0066580B+1333259]
Ordinal0 [0x00642314+1188628]
Ordinal0 [0x0064316F+1192303]
GetHandleVerifier [0x00907BF6+1548950]
GetHandleVerifier [0x009B461C+2256060]
GetHandleVerifier [0x0080C13B+518107]
GetHandleVerifier [0x0080B1E0+514176]
Ordinal0 [0x0071F53D+2094397]
Ordinal0 [0x00723418+2110488]
Ordinal0 [0x00723552+2110802]
Ordinal0 [0x0072CE81+2150017]
BaseThreadInitThunk [0x7602FA29+25]
RtlGetAppContainerNamedObjectPath [0x77467A9E+286]
RtlGetAppContainerNamedObjectPath [0x77467A6E+238]
Thanks in advance
Hope to here from you soon.
Where is the HTML? Without it, we can't really help you.
By the way, your user_id and user_password look like a class name, are you sure about find_element_by_id method?
Shouldn't you use find_element_by_class instead?
By seeing this error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="SignIn Form_email"]"}
You will have to make sure
This [id="SignIn Form_email"] is not under an iframe.
This [id="SignIn Form_email"] is not under a shadow-root.
You should not be on the new tab/windows launched by selenium.
You may be trying to interact with the element and it has not rendedered completely.
Also,
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
xpath that you should check :
[id="SignIn Form_email"]
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.
Now, if it is iframe, please switch to iframe first.
wait = WebDriverWait(driver, 30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "")))
if it is element not rendered issue, Please induce web driver wait like
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "")))
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I am practicing with selenium. I tried this page scraping with beautifulsoup, but as the page gets some time to load, I tried it with selenium with adding a delay time. I have tried drive.delay and timeout too. But nothing works fine with me. I get same alike error with all the tries, and I have attached my latest try and the error I get with it.
This is my ty:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
url = 'https://www.checkatrade.com/search?page=1&categoryId=1164&location=London'
driver =webdriver.Chrome(executable_path='D:\Web_Scraping\chromedriver_win32\chromedriver.exe')
driver.get(url)
driver.implicitly_wait(20)
items = driver.find_element_by_class_name('sc-8keyp7-0 kbPUFb sc-1a2v1qv-0 gwBNSh sc-3jeiz1-1 bfuahU')
for item in items:
company = item.find_element_by_xpath('//*[#id="1052063"]/div/div[2]/div[2]/a/div[1]/h2').text
print(company)
But it throws me this error, which I can not figure out.
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver =webdriver.Chrome(executable_path='D:\Web_Scraping\chromedriver_win32\chromedriver.exe')
DevTools listening on ws://127.0.0.1:1090/devtools/browser/9ed2753e-5d6e-4382-a9ac-a0ed2fbb8379
d:/Web_Scraping/seleniumtest.py:21: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
items = driver.find_element_by_class_name('sc-8keyp7-0 kbPUFb sc-1a2v1qv-0 gwBNSh sc-3jeiz1-1 bfuahU')
[5536:25384:1014/115806.197:ERROR:chrome_browser_main_extra_parts_metrics.cc(228)] crbug.com/1216328: Checking Bluetooth availability started. Please report if there is no report that this ends.
[5536:25384:1014/115806.198:ERROR:chrome_browser_main_extra_parts_metrics.cc(231)] crbug.com/1216328: Checking Bluetooth availability ended.
[5536:25384:1014/115806.199:ERROR:chrome_browser_main_extra_parts_metrics.cc(234)] crbug.com/1216328: Checking default browser status started. Please report if there is no report that this ends.
[5536:23016:1014/115806.200:ERROR:device_event_log_impl.cc(214)] [11:58:06.200] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[5536:25384:1014/115806.733:ERROR:chrome_browser_main_extra_parts_metrics.cc(238)] crbug.com/1216328: Checking default browser status ended.
Traceback (most recent call last):
File "d:/Web_Scraping/seleniumtest.py", line 21, in <module>
items = driver.find_element_by_class_name('sc-8keyp7-0 kbPUFb sc-1a2v1qv-0 gwBNSh sc-3jeiz1-1 bfuahU')
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 754, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "C:\Python38\lib\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":".sc-8keyp7-0 kbPUFb sc-1a2v1qv-0 gwBNSh sc-3jeiz1-1 bfuahU"}
(Session info: chrome=94.0.4606.81)
Stacktrace:
Backtrace:
Ordinal0 [0x0107BDE3+2473443]
Ordinal0 [0x01016661+2057825]
Ordinal0 [0x00F22438+1057848]
Ordinal0 [0x00F4C7DB+1230811]
Ordinal0 [0x00F76712+1402642]
Ordinal0 [0x00F6571A+1333018]
Ordinal0 [0x00F74ABF+1395391]
Ordinal0 [0x00F655AB+1332651]
Ordinal0 [0x00F42104+1188100]
Ordinal0 [0x00F42F59+1191769]
GetHandleVerifier [0x01202266+1549718]
GetHandleVerifier [0x012AD4A7+2250711]
GetHandleVerifier [0x0110718B+521403]
GetHandleVerifier [0x01106229+517465]
Ordinal0 [0x0101B79D+2078621]
Ordinal0 [0x0101FB58+2095960]
Ordinal0 [0x0101FC92+2096274]
Ordinal0 [0x01029541+2135361]
BaseThreadInitThunk [0x764DFA29+25]
RtlGetAppContainerNamedObjectPath [0x77A77A9E+286]
RtlGetAppContainerNamedObjectPath [0x77A77A6E+238]
What causes this error?
You are using a wrong locator.
find_element_by_class_name method is intended to receive a SINGLE class name while you are passing a multiple class names value to it "find_element_by_class_name".
To pass multiple class names you should use css selector or XPath.
So instead of
items = driver.find_element_by_class_name('sc-8keyp7-0 kbPUFb sc-1a2v1qv-0 gwBNSh sc-3jeiz1-1 bfuahU')
It could be
items = driver.find_element_by_css_selector('.sc-8keyp7-0.kbPUFb.sc-1a2v1qv-0.gwBNSh.sc-3jeiz1-1.bfuahU')
Or
items = driver.find_element_by_xpath("//div[#class='sc-8keyp7-0 kbPUFb sc-1a2v1qv-0 gwBNSh sc-3jeiz1-1 bfuahU']")