NoSuchElementException: Unable to find element - python

My ultimate goal is to simply login but there's 2 problem:
My code is unable to detect the ID to input email/password
The random characters in ID value keeps changing (id=login-email_k9et69del)
Your assistant will be greatly appreciated!
#from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
#create Chrome session
driver = webdriver.Chrome("C:\chromedriver.exe")
driver.get('https://www.jetblue.com/signin')
#login
#wait.until(EC.visibility_of_element_located((By.id, "emailLabel")));
username = driver.find_element_by_id("email").send_keys("xxx#gmail.com")
password = driver.find_element_by_id("Password").send_keys("xx")
screenshot inspect

The Id's that you are using are not correct.
You can use the below locators.
driver.find_element_by_css_selector("input[type='email']").send_keys("email goes here")
driver.find_element_by_css_selector("input[type='password']").send_keys ("password goes here")
You can check if the locators is correct in the browser dev tools. Check this post to know how to work with dev tools in browser dev tools.

Related

Taking screenshot of only relevant content of webpage | Selenium | Python

How can I take screenshot of only relevant content of any webpage using Selenium and Python?
I want to take the screenshot of the marked content (specifications) in this photo instead of whole page
Example webpage link
Currently I'm taking screenshot of the whole page. Also I want avoid referencing any class or id while taking the screenshot. Please let me know if I can achieve this (if yes, HOW?) or have to change my requirements. If there is any workaround such as cropping the relevant content, please do share too. Thanks.
Chrome appears to be a bit temperamental when screenshotting - only takes what's visible on screen, so I would advise Firefox/geckodriver for these kind of jobs. The following will take a full screenshot of that element:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
firefox_options = Firefox_Options()
firefox_options.add_argument("--width=1280")
firefox_options.add_argument("--height=720")
# firefox_options.headless = True
driverService = Service('chromedriver/geckodriver')
browser = webdriver.Firefox(service=driverService, options=firefox_options)
actions = ActionChains(browser)
url = 'https://www.startech.com.bd/benq-gw2480-fhd-monitor'
browser.get(url)
elem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[#id='specification']")))
elem.screenshot('fullspec.png')
print('screenshotted specs')

send.keys(Keys.ENTER) is not working in Python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
browser=webdriver.Chrome('C:/Users/Dell/Downloads/chromedriver')
browser.get('https://www.screener.in/')
sbox = browser.switch_to.active_element
sbox.send_keys('Infosys Ltd')
sbox.send_keys(Keys.RETURN)
The enter key is not working. I have tried using .submit() too but still isn't working. Please let me know if there is any other way to get it.
Try using Keys.ENTER instead of Keys.RETURN
url = "https://www.foodpanda.pk/restaurants/new?lat=24.9414896&lng=67.1676002&vertical=restaurants"
browser = webdriver.Chrome()
browser.get('https://www.screener.in/')
sbox = browser.switch_to.active_element
sbox.send_keys('Infosys Ltd')
WebDriverWait(browser, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '[class="dropdown-content visible"]')))
sbox.send_keys(Keys.ENTER)
wait for the dropdown to be visible before sending the enter
imports required:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
switch_to may be unstable depending on browsers. Also, make sure sbox is visible before you interact with it. Try:
sbox['value'].send_keys('Infosys Ltd')
sbox['value'].send_keys(Keys.RETURN)

Python Selenium Edgedriver stuck on showing "data;," in the address bar, not opening web page

I had this issue on 2 different systems, on first PC it went away after a reboot, on the other it had not gone away
Below is a portion of the code in question:
I masked some things on purpose
The edge browser is stuck on openign page with data;, shown in the address bar. It does not go beyond this.
I checked that Edge and Edgedriver are the same exact version, if it matters.
I cannot use Chrome or any other drivers for this project
Why doesn't selenium advance past the opening of the program?
I also have an error in CMD when it launches: [9704:11992:0305/080544.278:ERROR:storage_reserve.cc(164)] Failed to open file to mark as storage reserve: C:\Users**\AppData\Local\Temp\scoped_dir3468_1483298328\Default\Code Cache\js on selenium
How do I fix this?
# importing required package of webdriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.opera.options import Options
from selenium.webdriver.support.wait import WebDriverWait
import schedule
import time
from random import randint
def job():
while True:
# Instantiate the webdriver with the executable location of MS Edge
browser = webdriver.Edge(r"C:\Users\*****\Desktop\msedgedriver.exe")
sleep(2)
browser.maximize_window()
sleep(2)
browser.get('https://********/) #masked the name of website on purpose
try:
# Get the text box to insert Email using selector ID
myElem_1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'anti-myhub')))
sleep(3)
# Entering the email address
myElem_1.click()

Unable to populate value in Xpath

I am trying to login Naukri using python and selenium, I was able to access the login button using Xpath from the website https://www.naukri.com/, but I am unable to populate my mail ID in Email ID / Username, in the needed box. code I wrote
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.naukri.com/')
login = driver.find_element_by_xpath('//*[#id="login_Layer"]/div')
login.click()
userName = driver.find_element_by_xpath('//*[#id="root"]/div[2]/div[2]/div/form/div[2]/input')
userName.send_keys('jones1234#gmail.com')
I dont know, if I have the correct Xpath or not, my maild ID is not getting populated in. I think I have the incorrect Xpath, but I am not sure kindly help.
Add some wait prior to finding the element. You need to allow the element to popup after clicking the login.
wait = WebDriverWait(driver, 10)
driver.get('https://www.naukri.com/')
login = driver.find_element_by_xpath('//*[#id="login_Layer"]/div')
login.click()
userName=wait.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="root"]/div[2]/div[2]/div/form/div[2]/input')))
userName.send_keys('jones1234#gmail.com')
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Selenium - Edit Code using a userinterface

So i automated the task of login into a discordserver and posting a text using Selenium. All works fine but my goal is to make an inputbox for the email and password. The user inputs the information and presses save and the password and mail is supposed to write itsself into the code and save.
What are the best approaches for this? I'm still pretty new to python so go easy on me.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Firefox()
url = "https://discordapp.com/channels/530588470905929729/538868623981412362"
driver.get(url)
email = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='email']")))
email.send_keys("Mail#mail.com")
password = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='password']")))
password.send_keys("Password" + Keys.ENTER)
sleep(5)
textbox = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//textarea[#placeholder='Message #bot-commands']")))
textbox.send_keys("!work" + Keys.ENTER)
sleep(30)
driver.quit()

Categories

Resources