Selenium webdriver Message: no such element: Unable to locate element - iFrame [duplicate] - python

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.
I have a problem when want to click on number. Using Selenium webdriver.
Here is code
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
chromedriver_location = 'chromedriver'
driver = webdriver.Chrome(chromedriver_location)
driver.get('site')
username_input = '/html/body/div[2]/div/div/form/div[1]/input'
password_input = '/html/body/div[2]/div/div/form/div[2]/input'
login_submit = '/html/body/div[2]/div/div/form/button'
driver.find_element_by_xpath(username_input).send_keys('username')
driver.find_element_by_xpath(password_input).send_keys('password')
driver.find_element_by_xpath(login_submit).click()
numbers = '/html/body/div[1]/div/div[3]/div/div[1]/div[5]/div'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/iframe")))
driver.find_element_by_xpath(numbers).click()
This is in iFrame and need to click on number 5
Full xPath of number 5 is /html/body/div[1]/div/div[3]/div/div[1]/div[5]/div
Error message is:
Traceback (most recent call last):
File "<pyshell#25>", line 1, in
driver.find_element_by_xpath(numbers).click()
File "C:\Users\m\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\m\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\m\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\m\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":"/html/body/div1/div/div[3]/div/div1/div[5]/div"}
(Session info: chrome=87.0.4280.88)
Also tried with
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, numbers))).click()
Error for this is
Traceback (most recent call last):
File "<pyshell#26>", line 1, in
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, numbers))).click()
File "C:\Users\m\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

switch to iframe first:
iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/iframe")))
driver.switch_to.frame(iframe)
driver.find_element_by_xpath(numbers).click()
#REMAINING CODE TO INTERACT WITH ELEMENTS INISIDE IFRAME
#once done exit from iframe
driver.switch_to.default_content()

Related

XPATH element issue

I am facing a issue while calling xpath, i got the current xpath element by using selectorshub to get the xpath, but seems that not correct.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver_service = Service(executable_path="C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=driver_service)
PASSWORD = 'testtes'
login_page = 'http://192.168.2.1/login.html'
driver.get(login_page)
driver.find_element(By.XPATH,"//input[#placeholder='Password']").send_keys(PASSWORD)
Below is the error i'm getting
Traceback (most recent call last):
File "C:\Users\admin\Desktop\pyhton\index.py", line 13, in <module>
driver.find_element(By.XPATH, "//input[#placeholder='Password']").send_keys(PASSWORD)
File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute
self.error_handler.check_response(response)
File "C:\Python310\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":"xpath","selector":"//input[#placeholder='Password']"}
(Session info: chrome=107.0.5304.88)
Stacktrace:
Backtrace:
There is the screenshot of the HTML code from where i am trying to get the xpath from screenshot

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

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

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')

How to click on a username within web.whatsapp.com using Selenium and Python

The bot is supposed to send message on whatsApp WEB but unfortunately is stopping and giving error when asked find the user through X-path.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Chrome(executable_path="C:\drivers\chromedriver.exe")
driver.get("https://web.whatsapp.com/")
time.sleep(5)
name= input("Enter name")
input("Enter anything after scanning")
time.sleep(2)
user=driver.find_element_by_xpath("//span[#title='{}']".format(name))
The program is stopping exactly after this line, and giving the following error,
Traceback (most recent call last):
File "C:/Users/myName/PycharmProjects/firstpro/whatsAppBot.py", line 17, in <module>
user=driver.find_element_by_xpath("//span[#title='{}']".format(name))
File "C:\Users\myName\AppData\Local\Programs\Python\Python38-32\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\myName\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\myName\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\myName\AppData\Local\Programs\Python\Python38-32\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[#title='Jaden']"}
(Session info: chrome=81.0.4044.138)
Process finished with exit code 1
python version:3.8
Traceback (most recent call last):
File "C:/Users/myName/PycharmProjects/firstpro/whatsAppBot.py", line 17, in <module>
user=driver.find_element_by_xpath("//span[#title='{}']".format(name))
The final line of the traceback output tells you what type of exception was raised along with some relevant information about that exception. The previous lines of the traceback point out the code that resulted in the exception being raised.
here we see that it was unable to locate the path you specified
raison why it stopped
I feel the issue is with the element not being in the viewport, THe user you are trying to access must be not in the view, also try to debug the issue manually, by below steps
List item
try to locate the xpath //span[#title='Jaden'] and see if you are able to locate it in the dev tools without scrolling the page down.(If it is able to locate after scrolling the you will have to scroll programmatically using javascript executor.)
Try to see if there is any load time issue and try to implement appropriate explicit wait for the element
To send a message to a user through WhatsApp web https://web.whatsapp.com/ using Selenium you have to click on the username inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://web.whatsapp.com/")
name= input("Enter name:")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[role='option'] span[title='{}']".format(name)))).click()
Using XPATH:
driver.get("https://web.whatsapp.com/")
name= input("Enter name:")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#role='option']//span[#title='{}']".format(name)))).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

Can't Click on Span Link using Selinium Webdriver

Can't click on the download link using Selinium Webdriver
I'm trying to automate my weekly anime routes using python
from selenium import Webdriver
import time
dr = webdriver.Chrome()
time.sleep(3)
dr.get('https://horriblesubs.info/shows/one-punch-man-s2')
time.sleep(1)
link=dr.find_element_by_class_name("rls-info-container").click()
time.sleep(3)
blink=dr.find_element_by_xpath("//span[#class='dl_type hs-magnet-link']").click()
time.sleep(6)
dr.quit()
This is the error i get
Traceback (most recent call last):
File "C:\Users\Hey\AppData\Local\Programs\Python\Python37-32\Scripts\get-ShieldHero.py", line 16, in <module>
blink=dr.find_element_by_xpath("//span[#class='dl_type hs-magnet-link']").click()
File "C:\Users\Hey\AppData\Local\Programs\Python\Python37-32\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\Hey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Hey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Hey\AppData\Local\Programs\Python\Python37-32\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[#class='dl_type hs-magnet-link']"}
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.17134 x86_64)
I want to click on the 480 link, but I can't with the above code
I've attempted other variations where I try and inspect the html block, but I've been unsuccessful.
To click() on the link with text as Magnet for the item One Punch Man S2 you need to click the <a> node within the <span> node and you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'One Punch Man S2')]//following::span[#class='dl-type hs-magnet-link']/a"))).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

Categories