I need to make a login script for the website https://cbdbene.com/
but when I try to send Keys to the email field, I get the error
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I have tried using,
login_email = browser.find_element_by_xpath("//input[#id='login_email']")
driver.execute_script("argument[0].setAttribute('value', 'abs#gmail.com');", login_email)
but that is also of no help,
Even clicking the element has no response,
login_email = browser.find_element_by_xpath("//input[#id='login_email']")
driver.execute_script("argument[0].click();", login_email)
I don't know how to fill this form. Can someone please explain me what am I doing wrong here ?
Try below solution :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 50)
driver.get("https://cbdbene.com/")
warning = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='modal-dismiss']//i//*[local-name()='svg']")))
driver.find_element_by_tag_name('body').send_keys("Keys.ESCAPE")
warning.click()
loginIcon = wait.until(EC.element_to_be_clickable((By.XPATH, "//li[3]//span[1]//div[1]//div[1]//*[local-name()='svg']")))
loginIcon.click()
inputBox = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[4]//div[1]//div[1]//div[1]//div[1]//div[1]//div[1]//div[1]//span[1]//input[1]")))
inputBox.send_keys("Username")
Output:
If you use absolute xpath you will see 3 inputs elements are there with same property.
Induce WebDriverWait() and element_to_be_clickable() and use valid xpath.samething you have do with password.
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver=webdriver.Chrome()
driver.get("https://cbdbene.com/")
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.modal-dismiss"))).click()
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li.c-nav__list-item>span.c-nav__link"))).click()
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"(//input[#id='login_email'])[last()]"))).click()
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"(//input[#id='login_email'])[last()]"))).send_keys("user name")
Browser snapshot:
Related
Code trials:
from selenium import webdriver
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.chrome.service import Service
from time import time
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
service = Service(executable_path="C:\\Users\\aps\\Desktop\\Python\\chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://youtube.com")
search = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "search")))
ActionChains(driver).move_to_element(search).pause(2).click_and_hold().send_keys("Iktarfa").perform()
button = driver.find_element(By.ID, "search-icon-legacy").click()
Search field is getting fetched.
But after that I am getting following error, I am out of ideas and a new learner. Please HELP!!
The locator strategy which you have used:
(By.ID, "search")
identifies multiple elements within the HTML DOM.
and the first WebElement having the property style="display: none;". Hence you see the error.
Solution
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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ytd-searchbox#search"))).send_keys("text")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytd-searchbox[#id='search']"))).send_keys("text")
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
There is no need to add a Actions class for this scenario. It is perfectly capable of doing the search after just using the click and then searching for the element.
I used this code piece and it works perfectly fine
import time
from seleniumwire import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
svc = Service(ChromeDriverManager().install())
options = Options()
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(service=svc,options=options)
driver.maximize_window()
driver.get("https://youtube.com")
search = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "search")))
driver.find_element(By.NAME,'search_query').click()
time.sleep(5)
driver.find_element(By.NAME,'search_query').send_keys('Iktarfa')
button = driver.find_element(By.ID, "search-icon-legacy").click()
time.sleep(5)
driver.quit()
I've used the sleep method, but you should in general use an intelligent wait mechanism like Explicit waits.
Output screenshot -
from selenium import webdriver
userName = "myusername123"
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/")
driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input').send_keys(userName)
Error
Unable to locate element: {"method":"xpath","selector":"//*. [#id="loginForm"]/div/div[1]/div/label/input"}
I'm trying to make a login in Instagram using selenium but I can't reach the username box with XPath
You could check with the explicitWait and use the name attribute
username=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//input[#name='username']")))
username.send_keys("userName")
password=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//input[#name='password']")))
password.send_keys("password")
import
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
See, there is name attribute available, you can try with that as below :-
driver.find_element_by_name('username').send_keys(userName)
name is always a reliable locator than xpath
if the above does not work, you can try with the Explicit waits as well :-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
userName = "myusername123"
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.NAME, 'username'))).send_keys(userName)
wait.until(EC.element_to_be_clickable(( By.NAME, "password"))).send_keys('somepassword')
wait.until(EC.element_to_be_clickable(( By.XPATH, "//div[text()='Log In']/.."))).click()
I am trying to enter username and password in the following website:
https://www.thegreatcoursesplus.com/sign-in
driver = webdriver.Chrome()
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
driver.find_element_by_xpath('//h1[#class="sign-in-input"]').click()
This gave following exception:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
Then I tried to use java script:
driver.execute_script("document.getElementsByClassName('sign-in-input')[0].click()")
cmd = "document.getElementsByClassName('label-focus')[0].value = 'abc#abc.com'"
driver.execute_script(cmd)
There are no errors but no text is sent to "Email Address" field.
Can someone please guide me on the correct way to enter email address, password and then click "Sign-in".
This error message...
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
...implies that the desired element was not visible within the HTML DOM while the WebDriver instance was trying to find it.
ElementNotVisibleException
ElementNotVisibleException is thrown when an element is present on the DOM Tree, but it is not visible, and so is not able to be interacted with.
Reason
One possitive take away from ElementNotVisibleException is the fact that the WebElement is present within the HTML and this exception is commonly encountered when trying to click() or read an attribute of an element that is hidden from view.
Solution
As ElementNotVisibleException ensures that the WebElement is present within the HTML so the solution ahead would be two folds as per the next steps as detailed below:
If you next step is to read any attribute of the desired element, then you need to induce WebDriverWait in-conjunction with expected_conditions clause set to visibility_of_element_located as follows:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
my_value = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "element_xpath"))).get_attribute("innerHTML")
If you next step is to invoke click() on the desired element, then you need to induce WebDriverWait in-conjunction with expected_conditions clause set to element_to_be_clickable as follows:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).click()
This usecase
The xpath you constructed as //h1[#class="sign-in-input"] doesn't match any node. We need to create unique xpath to locate the elements representing Email Address, Password and Sign In button inducing WebDriverWait. The below code block will help you to achieve the same:
from selenium import webdriver
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")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='modal']//input[#name='email']"))).send_keys("abc#abc.com")
driver.find_element_by_xpath("//div[#id='modal']//input[#name='password']").send_keys("password")
driver.find_element_by_xpath("//div[#id='modal']//button[#class='color-site sign-in-button']").click()
for username use :
driver.find_element_by_xpath("//input(#type='email')").click()
driver.find_element_by_xpath("//input(#type='email')").send_keys( "username" )
for password use :
driver.find_element_by_xpath("//input(#type='password')").click()
driver.find_element_by_xpath("//input(#type='password')").send_keys( "password" )
There are two problems:
The first one is timing, it takes some time for the form to appear. You you can use explicit wait to solve it.
The second one is that the field id in <input> tag, not <h1> tag, and there are many fields that matches this xpath. I suggest you locate the form holding the fields and use it to locate each field
For the timing issue you can use explicit wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://www.TheGreatCoursesPlus.com/sign-in')
form = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[#class="modal-body"]//form')))
form.find_element_by_name('email').send_keys(email)
form.find_element_by_name('password').send_keys(password)
form.find_element_by_name('sign-in-button').click()
Here is my code so far, note that the web page loads up with a captcha. I countered this with adding a time.sleep so I can run tests. When I try to submit the form by submitting "Create new account", I get an error saying the element has no attribute for 'submit'. I tried finding the element using xpath, css_selectos, tags, class names, etc.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
browser = webdriver.Chrome()
browser.get('https://www.bstn.com/en/register/address')
time.sleep(35)
elam = browser.find_element_by_css_selector("[value='Create new account']")
elam.Submit()
If you are trying to click on Create new account button after filling information then please find below xpath to click on it
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[#class='button radius charcheck-submit']"))).click()
Another solution with action class
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ActionChains
actionChains = ActionChains(driver)
submit = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//input[#class='button radius charcheck-submit']")))
actionChains.move_to_element(submit).click().perform()
Working code
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
# browser = webdriver.Chrome()
browser.get('https://www.bstn.com/en/register/address')
time.sleep(35)
WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[#class='button radius charcheck-submit']"))).click()
I try to write a short script to search a key word on newspaper's website and an
'ElementNotVisibleException: Message: element not visible' is raised.
I'm not able to fixe it...
Thank you for help
code:
import os
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
driver = webdriver.Chrome()
driver.get("https://www.tsa-algerie.com")
wait = WebDriverWait(driver, 8)
elem = driver.find_element_by_name("s")
wait.until(EC.visibility_of_element_located((By.name,"s")))
elem.send_keys("Algieria")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
os.system('pause')
You just made a variable called wait:
wait = WebDriverWait(driver, 8)
But you didn't use it in your code. Try this:
visibility_of_element_located
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver, 8).until(EC.visibility_of_element_located((By.NAME, "s")))
elem = driver.find_element_by_name("s")
That way script will wait until 's' element appear, then u gonna find this element. Remember about the order. First u wait, next you can use it.
This code will help you:
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
driver = webdriver.Chrome()
driver.get("https://www.tsa-algerie.com")
driver.maximize_window()
#To click on Search Icon to open the search box
ele=driver.find_element_by_xpath('//div[#class="template-header__search search__open transition"]/img')
ele.click()
#wait till search text box appear and then enter the desired keyword you want to search
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, "s")))
elem = driver.find_element_by_name("s")
elem.send_keys("Algieria")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()