Can't locate this input element selenium - python-3.x

I am trying to locate the input “to” field on this webpage: Link here
(sorry about having to login to see it)
My code:
name_field = driver.find_element_by_xpath("//form[#id='compose-message']/div[6]/div/div/div[2]/div/div/textarea")
But that gives the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//form[#id='compose-message']/div[6]/div/div/div[2]/div/div/textarea"}
(Session info: chrome=93.0.4577.63)
For some reason this page is the only page that has given me any issue.

Most of the textarea web elements are wrapped inside iframes.
Using explicit waits, does give your script a stability that it need.
Also prefer to use css selector over xpath.
In Selenium, to access elements which are inside of iframe, we need to switch the focus of webdriver to iframe. Like below
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src^='/message/compose/']")))
and once you are inside this iframe you can interact with to input web element. Like below
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.NAME, "to"))).send_keys('something here')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='message']/following-sibling::div/descendant::textarea"))).send_keys('Some message')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Also, remember to switch to default content when you are done with iframe.
driver.switch_to.default_content()
In order to work with elements which are outside of this iframe.

The Element you are trying to find is in an iframe. Was able to find and send text to the elements with below code.
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import time
opt = ChromeOptions()
opt.add_argument("--user-data-dir=C:\\Users\\*****\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path="path to chromedriver.exe",options=opt)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://www.reddit.com/message/compose/")
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[#class='saPujbGMyXRwqISHcmJH9']"))
driver.find_element_by_xpath("//input[#name='to']").send_keys("Sample Text")
driver.find_element_by_xpath("//div[#class='usertext']//textarea").send_keys("Sample Text")
time.sleep(5)
driver.quit()

First of all that element is inside an iframe. To access it you need to switch to that iframe. Also, your locator are not looking good.
Please try the following code:
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(#src,'message')]")))
name_field = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[#name='to']")))
To use the above expected conditions you will need the following imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

use command CSS selector instead of XPath such as use below code.
yourwebsriver.find_elements_by_css_selector("textarea[name=text]").send_keys('something here')

Related

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference error while sending text to Search box in YouTube

I would like to create a code snippet for opening YouTube, accepting the cookies, finding the search bar, typing some string into it and finally clicking on the search button. Its not too hard but something is not working. I have tried using the WebDriverWait as well but still not working.
If I open the Google (of course from code) and doing the same procedure then everything works well. I have tried finding elements not only XPATH but also ID, and CSS_SELECTOR.
After the send_keys() function the error message is:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
My code is:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_experimental_option('detach', True)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.get("https://www.youtube.com/")
driver.maximize_window()
cookie_accept = driver.find_element(By.XPATH, '//*[#id="content"]/div[2]/div[6]/div[1]/ytd-button-renderer[2]/yt-button-shape/button/yt-touch-feedback-shape/div/div[2]')
cookie_accept.click()
yt_searchbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search"]')))
yt_searchbox.send_keys('Python Selenium')
I've tried without the WebDriverWait as well:
yt_search = driver.find_element(By.XPATH, '//*[#id="search"]')
yt_search.send_keys("Pyhton Selenium")
And it is not working either. I don't know what's going on.
The Search box within Youtube homepage is a dynamic element, so ideally to send a character sequence to the 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:
driver.get('https://www.youtube.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys("Python Selenium")
Using XPATH:
driver.get('https://www.youtube.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='search']"))).send_keys("Python Selenium")
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:

Python/Selenium Access element inside of iframe

I'm trying to access some elements inside of iframe but withour success. Basically, I found the iframe, switch to it, but I can get the element inside of it. All elements that I try, I always get a message saying that the element was not found.
Could you please help?
HTML:
You can use the below code to switch to frame and then interact with the desired element :-
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(#sandbox,'allow-popups-to-escape-sandbox')]")))
ele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.analytics-ui-application")))
#ele is a web element so you can trigger .click, .text or any other web element methods on it.
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
As the elements are within an <iframe> 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:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame-router[activeclient='analyticsUi'] > iframe[sandbox]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "css_clickable_element"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame-router[#activeclient='analyticsUi']/iframe[#sandbox]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "xpath_clickable_element"))).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
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

Cant Find Element in Selenium Python

Hello I am trying to use selenium to find a button to click on. Below is a snippet of the HTML code i am working with.
<input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()">
I am trying to click on the runButton with the code below.
elem = driver.find_element_by_id('runButton').click()
I am getting the following error message:
NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]
Not sure what else to try.
Most likely what you'll need to do to find your element is to use waits. You need to allow time for an element to be visible, clickable, etc. before you can interact with it. You can find information on waits here: https://selenium-python.readthedocs.io/waits.html
Taken from the above website:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "runButton"))
If waits do not work then it's possible that your element is inside an iframe. You will need to switch to that iframe first and then search for the element in order to find it.
You will find the iframe like you would another element and then switch to it like this:
iframe = driver.find_element_by_id("content_Iframe")
driver.switch_to.frame(iframe)
button = driver.find_element_by_id("runButton")
button.click()
Once you're done with the iframe and it's contents, you will need to switch back out of it:
driver.switch_to.default_content()
The element seems to be a dynamic element so to click() on the element you need to use 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, "input.button#runButton[value='Run Report']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='button' and #id='runButton'][#value='Run Report']"))).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

How to check a checkbox in Chrome using Selenium with python

I'm trying to check a checkbox via selenium in Chrome using python3.
This is the HTML code:
<header class="list-header">
<aside class="list-header-bulk-selection">
<input type="checkbox" class="sc-cSHVUG iAwiCZ">
::after
I'm trying to check the box by:
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
check_mark.click()
I am able to find the location, but unfortunately I get the following error message:
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=75.0.3770.142)
I think I have to access the ::after line, but I have no clue how I should do this.
Try following options to click on checkbox.
Option1:
location_once_scrolled_into_view
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
check_mark.location_once_scrolled_into_view
check_mark.click()
Option2:
WebDriverWait and element_to_be_clickable
check_mark =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#class='sc-cSHVUG iAwiCZ']")))
check_mark.click()
Option3:
Java Scripts Executor
check_mark =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#class='sc-cSHVUG iAwiCZ']")))
driver.execute_script("arguments[0].click();", check_mark)
You need to import followings to execute above code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
This sc-cSHVUG iAwiCZ looks suspicious and most probably it's automatically generated each time you refresh the page. I would recommend to stick to something more "static", for example the checkbox should have some label or other text explaining what it does and you could stick to that text.
From what you've provided so far it might be better to locate this <aside> tag first and then use child axis to reach the checkbox like:
//aside[contains(#class, 'bulk-selection')]/child::input
More information:
XPath Tutorial
XPath Axes
XPath Operators & Functions
The desired element is a dynamic element so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "header.list-header>aside.list-header-bulk-selection>input[type='checkbox']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//header[#class='list-header']/aside[#class='list-header-bulk-selection']/input[#type='checkbox' and #class]"))).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
Thank you guys so much. The thing was that ::after is a pseudo-element which is not interactable by selenium.
I actually solved it by accessing the checkbox and then hoovering over it (just a tiny bit) to sort of activate the HTML object. Then the checkbox was interactable.
from selenium.webdriver import ActionChains
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
ActionChains(driver).move_to_element_with_offset(check_mark,0.001,0.001).click().perform()

Python, unable to find element with class name

First, i'm an absolute beginner about this, have been trying to locate an element on a web page (lg.telin.co.id/lgnew/cacti.php), this element has the exact name when i wrote the code which is "login_username". However, i got the message " Unable to locate element " until now. Also this web page does not have any id
i used find_element_by_name but nothing work so far
from selenium import webdriver
browser=webdriver.Firefox(executable_path="C:\\Program Files (x86)\\Python37-32\\BrowserDriver\\geckodriver.exe")
browser.get("https://lg.telin.co.id/lgnew/cacti.php")
usernameStr = '123456'
username = browser.find_element_by_xpath("/input[#name='login_username']")
username.send_keys(usernameStr)
if the code work, text will be filled with characters i assume
Try switch to iframe before handling form input fields:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser.switch_to.frame('cacti')
username = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.NAME, 'login_username')))
username.send_keys(usernameStr)
You were pretty close. As the the desired elements are within an <iframe> 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 the following solution:
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='cacti']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login_username']"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='cacti']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='login_username']"))).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
Here you can find a relevant discussion on Ways to deal with #document under iframe

Resources