0
votes

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.exceptions.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 :)

2
Could you send through a formatted version of your code... This is very difficult to understand and interpret. - James Cook
please always parse your code and your error. The error is probably because the element you are searching doesn't exist. You have to check the xpath to be sure you are searching it right - Flavio Moraes
@JamesCook pastebin.com/XNL1qsYv sorry,u can check there - kizuya
It look like your trying to toggle a dynamic class. - Arundeep Chohan
@arundeepchohan what should i do then? - kizuya

2 Answers

0
votes

You can simply target this xpath and .click to toggle the less secure apps.

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

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