I am trying to make a python program using Selenium to automate the UnovaRPG game. It is supposed to do the following in order:
1) Login with username and password.
2) Go to heal center and click heal.
3) Fight the trainer "shedinja144" (using given link)
4) Select a specified pokemon.
5) During the fight, it should click the last attack followed by continue.
6) Then click "Back to map" for the last pokemon (The 6th).
7) Repeat steps 2-5 any given number of times.
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.unovarpg.com/login.php")
assert "UnovaRPG" in driver.title
i=1
j=1
typeUsername = driver.find_element_by_id("username")
typePassword = driver.find_element_by_id("password")
typeUsername.send_keys("******")
typePassword.send_keys("******")
driver.find_element_by_id("buttonLogin").click()
while i<10:
driver.get("https://www.unovarpg.com/pokemon_center.php")
driver.find_element_by_id("healButton").click()
driver.get("https://www.unovarpg.com/battle.php?type=autotrainer&tid=5546527")
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img").click()
while j<6:
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[4]/div[6]/div[2]/ul/li[4]/a/strong").click()
driver.find_element_by_id("continue").click()
j+=1
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[4]/div[6]/div[2]/ul/li[4]/a/strong").click()
driver.find_element_by_id("Back2MapButton").click()
i+=1
Here is the error:
Traceback (most recent call last):
File "Selenium v1.py", line 21, in <module>
driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img").click()
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 230, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 662, in find_element
{'using': by, 'value': value})['value']
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "E:\Programming\Python\Portable Python 2.7.6.1\App\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/driver-component.js:9641:26)
at FirefoxDriver.prototype.findElement (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/driver-component.js:9650:3)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/command-processor.js:11635:16)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/command-processor.js:11640:7)
at DelayedCommand.prototype.execute/< (file:///c:/users/adb/appdata/local/temp/tmpo1kmq3/extensions/fxdriver#googlecode.com/components/command-processor.js:11582:5)
***Repl Closed***
The error lies in the inability to find the image of the pokemon to click (Step 4). I have used the xcode path copied from the Firebug inspector.
Help would be much appreciated.
Please note that this is just for creative purposes and will not be used to "cheat".
You need to explicitly wait for the element to load before interacting with it:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div[3]/div/div/div[2]/div[2]/div[8]/div[4]/div[1]/img"))
)
Related
I'm trying to get access to this page "fullcollege" with a bot I'm making for students. The problem is that I can't even select an element from it, and this error shows up. I have recently tested my code with another webpages like instagram and everything works perfectly. Anyone knows what can I do to solve this? Thanks in advance.
My code:
from time import sleep
from selenium import webdriver
browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('https://www.fullcollege.cl/fullcollege/')
sleep(5)
username_input = browser.find_element_by_xpath("//*[#id='textfield-3610-inputEl']")
password_input = browser.find_element_by_xpath("//*[#id='textfield-3611-inputEl']")
username_input.send_keys("username")
password_input.send_keys("password")
sleep(5)
browser.close()
The error:
Traceback (most recent call last):
File "c:\Users\marti\OneDrive\Escritorio\woo\DiscordBot\BetterCollege\tester.py", line 11, in <module>
username_input = browser.find_element_by_xpath("//*[#id='textfield-3610-inputEl']")
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\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\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[#id='textfield-3610-inputEl']
The username and password field is inside and iframe you need to switch it first.
browser.get('https://www.fullcollege.cl/fullcollege/')
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#logFrame")))
sleep(5)
username_input = browser.find_element_by_xpath("//input[#id='textfield-3610-inputEl']")
password_input = browser.find_element_by_xpath("//input[#id='textfield-3611-inputEl']")
username_input.send_keys("username")
password_input.send_keys("password")
import below libraries as well.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
I'm using Selenium with Python to input an address into a textbox within the Glovo page for Madrid. The code I wrote can be seen below, and the error I get is also copied after the code.
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
import time
url = 'https://glovoapp.com/es/es/madrid/'
# open browser
driver = webdriver.Chrome()
# load page
driver.get(url)
# find field
item = driver.find_element_by_class_name('address-input__container')
# select textbox and input text
time.sleep(2)
item.click()
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "el-input__inner"))
)
item = driver.find_element_by_class_name('el-input__inner')
time.sleep(2)
item.send_keys('Calle')
The error I get is shown below.
Traceback (most recent call last):
item.send_keys('Calle')
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Usuario\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
`ElementNotInteractableException: element not interactable`
(Session info: chrome=91.0.4472.124)
Locator you are using is returning 2 different elements. See if this works
item = driver.find_element_by_xpath("//input[#class='el-input__inner' and #data-test-id='address-input-autocomplete']")
item.send_keys("Calle")
Using various xpath for the element address-input__containerand trying to click it is not working while automation and throws ElementNotInteractableException: Message: element not interactable You can refer this question. Applying the same and it works.
from selenium.webdriver.common.action_chains import ActionChains
driver.get("https://glovoapp.com/es/es/madrid/")
box = driver.find_element_by_class_name('address-input__container')
time.sleep(2)
ActionChains(driver).move_to_element(box).click(box).perform()
driver.find_element_by_xpath("//input[#data-test-id='address-input-autocomplete']").send_keys("SampleText")
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 :)
You can simply target this xpath and .click to toggle the less secure apps.
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
I know this has been asked lots of times before but how do you get around the "element not intractable" exception?
I'm pretty new to Selenium so excuse me if I get something wrong or misunderstood.
I have tried adding time.sleep(20) in various parts of the code to see if this allows the element to load but no success as yet.
Am I missing something here?
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
#Login Credentials
email = 'anexample#fakeemail.com'
password = 'Password123'
#Login to Money Dashboard
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get("https://my.moneydashboard.com/")
loginPageEmail = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="input_0"]')))
loginPageEmail.send_keys(email)
I always get an error along the lines of:
Traceback (most recent call last):
File "mdash.py", line 26, in <module>
loginPageEmail.send_keys(email)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py",
line 479, in send_keys
'value': keys_to_typing(value)})
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py",
line 633, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 321, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=75.0.3770.100)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Mac
OS X 10.13.6 x86_64)
If you check the element id attribute is dynamic every time you run the code.So name attribute should be unique attribute here to access the input element .However Without form element it is not identifying the input element so I have used the form element along with input element and unique property of input element.
Use WebdriverWait and elementtobeclickable and following xpath.
email = 'anexample#fakeemail.com'
password = 'Password123'
#Login to Money Dashboard
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get("https://my.moneydashboard.com/")
loginPageEmail = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//form[#name="vm.registerForm"]//div[#class="inputs"]//input[#name="email"]')))
loginPageEmail.send_keys(email)
loginPagepassword = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//form[#name="vm.registerForm"]//div[#class="inputs"]//input[#name="password"]')))
loginPagepassword.send_keys(password)
Output:
So im pretty new to selenium and im following the docs to make some bots,
but when i try to login into social media networks (twitter/instagram) it doesnt send the strokes.
Code:
#!usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get("https://www.instagram.com/accounts/login/")
elem = browser.find_element_by_name("username")
elem.send_keys('Laptops' + Keys.RETURN)
time.sleep(4)
browser.quit()
i've tried it by using- browser.get_element_by_name/class/xpath but nothing seemed to work.
Error code:
Traceback (most recent call last):
File "ig.py", line 50, in <module>
login(driver)
File "ig.py", line 15, in login
driver.find_element_by_xpath("//div/input[#name='username']").send_keys(username)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 843, in find_element
'value': value})['value']
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //div/input[#name='username']
os=ubuntu17
driver=firefox/geckodriver
python3.6
selenium3.6
I know this code shouldnt work bc you need a password and a username, but it doesnt even execute the send_keys code because of the error on the line above tHAT
I've had some problems with Firefox driver on linux too.
I copied your code and only changed the driver to Chrome and it worked.
You can get the Chrome driver here:
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.webdriver
Try adding a wait-for presence condition. It may however require a EC.element_to_be_clickable, if the element is disabled until some check is performed.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
...
usernamefield = WebDriverWait(self.driver, 10)\
.until(EC.presence_of_element_located((By.NAME, 'username')))
usernamefield.send_keys("Laptops")
passwordfield = WebDriverWait(...