Why does selenium not work on my browsers? [duplicate] - python

This question already has answers here:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable when clicking on an element using Selenium Python
(6 answers)
Closed 2 years ago.
I want to automate through python using selenium and I need help.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
Sergo=['Sergo']
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/")
print(driver.title)
search = driver.find_element_by_xpath('//*[#id="search"]')
search.send_keys(Sergo)
time.sleep(5)
driver.quit()
Here is the error:
Traceback (most recent call last):
File "c:/Users/user/Desktop/bot/automation.py", line 11, in <module>
search.send_keys(Sergo)
File "C:\Users\user\Desktop\bot\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\user\Desktop\bot\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\user\Desktop\bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\user\Desktop\bot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=83.0.4103.116)

Your xpath is not unique
Use this xpath:
search = driver.find_element_by_xpath('//input[#id='search']')

This is because you don't have the Chromedriver.
Click on the 3 dots at the right-top in your Chrome browser
Click on About Chrome/Chromium
Check the version mentioned in light gray
Go to the ChromeDriver [downloads page]
(https://chromedriver.chromium.org/downloads).
Download the correct version as per your browser version.
Copy the path to ChromeDriver.exe
As an argument,pass executable_path=r'path/to/chromedriver.exe'
in driver = webdriver.Chrome().
For example, mine is:
driver = webdriver.Chrome(executable_path=r'C:\Users\dell\Libs\chromedriver.exe')

Related

Unable to send WhatsApp message using chrome driver Python Error: NoSuchElementException

This is my first post here!
I am using Selenium's Chrome Driver to send WhatsApp attachment to some people.
Here is my code:
from selenium import webdriver
import os
from time import sleep
link="https://wa.me/91xxxxxxxxxx"
phnno="91xxxxxxxxxx"
driver=webdriver.Chrome(executable_path=f"{os.getcwd()}\\chromedriver.exe")
#driver.get(link)
#button=driver.find_element_by_xpath('//a[#title="Share on WhatsApp"]')
#button.click()
driver.get(f"https://web.whatsapp.com/send?phone={phnno}&text&app_absent=0")
#This above line opens up the sender window in whatsapp
attachbutt=driver.find_element_by_xpath('//span[#data-icon="clip"]') #This is line 15
#The above line is the one that is giving me the error
attachbutt.click()
sleep(10)
forpdf=driver.find_element_by_xpath('//input[#accept="*"]')
path="C:\\Users\\Deven\\Desktop\\test_file.pdf"
forpdf.send_keys(path) #Attaches the file
sleep(5)
sendbutt=driver.find_element_by_xpath('//span[#data-icon="send"]')
sendbutt.click()
ERROR:
DevTools listening on ws://127.0.0.1:56230/devtools/browser/1a8a2adb-37ee-4b0c-bedc-5cfb58559c24
Traceback (most recent call last):
File "d:\Coding\Python Scripts\Dr Nikhil Prescription App\Prescription Generator\WA-testing.py", line 15, in <module>
attachbutt=driver.find_element_by_xpath('//span[#data-icon="clip"]')
File "C:\Users\Deven\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Deven\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Deven\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Deven\AppData\Local\Programs\Python\Python39\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":"xpath","selector":"//span[#data-icon="clip"]"}
(Session info: chrome=90.0.4430.212)
PS D:\Coding\Python Scripts\Dr Nikhil Prescription App\Prescription Generator> [16176:15752:0523/212201.236:ERROR:device_event_log_impl.cc(214)] [21:22:01.236] USB: usb_device_handle_win.cc:1054 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
It says it is unable to locate the element but I have bene very careful in inspecting the website and then writing the code. Still I wonder why it does not work. Can anyone please help me with what is it that I am doing wrnong? Thank You!
Looks like you are missing a wait / delay before clicking that element.
The simplest solution is to put
sleep(5)
before
attachbutt=driver.find_element_by_xpath('//span[#data-icon="clip"]')
However, it's much better to use expected conditions there. Like this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
attachbutt = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//span[#data-icon="clip"]')))

What am i doing wrong. Selenium phyton [duplicate]

This question already has answers here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
"selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element" while clicking a 'Next' button with Selenium
(1 answer)
Selenium in Python: "NoSuchElementException: Message: no such element: Unable to locate element"
(5 answers)
Closed 2 years ago.
Is it because i using python REPL? I don't know what that is.
My code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://member.lazada.com.my/user/login?spm=a2o4k.home.header.d5.1e532e7eD9UgkJ&redirect=https%3A%2F%2Fwww.lazada.com.my%2F%3Fspm%3Da2o4k.login_signup.header.dhome.7d0d49fbWgHyQw")
print(driver.title)
time.sleep(10)
driver.find_element_by_id("a2o4k.login_signup.menu.i0.128549fbdpzNIi").click()
Error code:
Lazada.com.my: Online Shopping Malaysia - Mobiles, Tablets, Home Appliances, TV, Audio & More
Traceback (most recent call last):
File "C:\Program Files (x86)\CUBAR.py", line 13, in <module>
driver.find_element_by_id("a2o4k.login_signup.menu.i0.128549fbdpzNIi").click()
File "C:\Users\naufa\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\naufa\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\naufa\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\naufa\AppData\Local\Programs\Python\Python39\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":"css selector","selector":"[id="a2o4k.login_signup.menu.i0.128549fbdpzNIi"]"}
(Session info: chrome=87.0.4280.66)
You're trying to find an id that is dynamic and will change instead use either a css selector or xpath. I am assuming you are trying to click the sign up button or something similar. Here's an example of that element.
CSS SELECTORS
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.mod-login-btn > button"))).click()
XPATHS
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "button[text()='SIGN UP']"))).click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Unable to find element and input value using selenium

My objective is to take a value (in my case a shipment tracking #) and input it into a tracking field on a website using selenium. I am unable to input the value and get the following error message:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "/Users/GUVA/Downloads/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.17track.net/en")
#data = df.values[1] # grabbing one tracking number from an excel file
# click "agree" to close window
python_button = driver.find_elements_by_xpath('//*[#id="modal-gdpr"]/div/div/div[3]/button')[0]
python_button.click()
# enter tracking number into text box
que=driver.find_element_by_id('//input[#id="jsTrackBox"]')
que.send_keys("data")
Traceback (most recent call last):
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/Track Shipment.py", line 26, in <module>
que=driver.find_element_by_id('//input[#id="jsTrackBox"]')
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=83.0.4103.61)
Any suggestions please?
//input[#id="jsTrackBox"] is an xpath. So you need to fetch the element by using find_element_by_xpath method(where you are currently using find_element_by_id method).
Your code should be like:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "/Users/GUVA/Downloads/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.17track.net/en")
#data = df.values[1] # grabbing one tracking number from an excel file
# click "agree" to close window
python_button = driver.find_elements_by_xpath('//*[#id="modal-gdpr"]/div/div/div[3]/button')[0]
python_button.click()
# enter tracking number into text box
que=driver.find_element_by_xpath("//div[#id='jsTrackBox']//div[#class='CodeMirror-scroll']")
que.click()
que.send_keys("data")

Python selenium unable to find whatsapp web textarea by xpath

I'm building a whatsapp automation script using python selenium. I'm unable to find the message text area to send message.
My code :
from selenium import webdriver
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import os
import mysql.connector
driver = webdriver.Chrome('/Users/evilslab/Downloads/chromedriver 3')
driver.get('https://web.whatsapp.com/send?phone=0097155960&text&source&data&app_absent')
time.sleep(10)
text = "Hello testing"
inp_xpath = '//*[#id="main"]/footer/div[1]/div[2]'
input_box = driver.find_element_by_xpath(inp_xpath)
time.sleep(2)
input_box.send_keys(text + Keys.ENTER)
time.sleep(2)
The terminal is :
Traceback (most recent call last):
File "/Users/evilslab/Documents/Websites/www.futurepoint.dev.cc/dobuyme/classy/whatsapp-selenium.py", line 18, in <module>
input_box = driver.find_element_by_xpath(inp_xpath)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
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":"xpath","selector":"//*[#id="main"]/footer/div[1]/div[2]"}
(Session info: chrome=81.0.4044.138)
May be your xpath is wrong. When I inspect and get xpath I get //*[#id="main"]/footer/div[1]/div[2]/div/div[2]
i guess there is a problem with the xpath
find how to get specific xpath from here Is there a way to get the XPath in Google Chrome? , sometimes whatsapp change their class name

Error while scrolling to the bottom of a Web page using selenium

I'd like to scroll to the bottom of a page using Selenium with the Firefox driver.
Here is what I have so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser=webdriver.Firefox()
browser.get('http://nostarch.com')
htmlElem=browser.find_element_by_tag_name('html')
htmlElem.send_keys(Keys.END) #scrolls to bottom
Unfortunately, my code fails with the following exception:
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
htmlElem.send_keys(Keys.END) #scrolls to bottom
File "C:\Users\academy\AppData\Local\Programs\Python\Python35\lib\site-
packages\selenium\webdriver\remote\webelement.py", line 352, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\academy\AppData\Local\Programs\Python\Python35\lib\site-
packages\selenium\webdriver\remote\webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "C:\Users\academy\AppData\Local\Programs\Python\Python35\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "C:\Users\academy\AppData\Local\Programs\Python\Python35\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 194, in
check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message:
Element is not visible
Use WebDriverWait (explicit wait) till element to be visible i dont know too much about python so give just general syntax
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser=webdriver.Firefox()
browser.get('http://nostarch.com')
htmlElem=WebDriverWait(browser, 5).until(
EC.presence_of_element_located((By.find_element_by_tag_name, "html"))
htmlElem.send_keys(Keys.END)
To scroll to the bottom of a page using Selenium you can use :
Python
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Java
((JavascriptExecutor)driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");

Categories