Open another page after logging in using selenium webdriver - python

I am using selenium webdriver to open a page in my website using python.
The page link is : "www.example.com/App/Details.aspx/I=5aM%+8KbCv1o=&T=M30Lr7RtcdR=&H=fRFKse5fKA=="
Since I cannot open this page directly, I have to first login on
www.example.com, which I am doing this way:
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
try:
driver.set_page_load_timeout(10)
driver.get('http://www.example.com')
except Exception:
print ('time out')
#driver.find_element_by_id("Default").send_keys(Keys.CONTROL +'Escape')
username = driver.find_element_by_id("usrid")
password = driver.find_element_by_id("pswd")
username.send_keys("Sample_user")
password.send_keys("samplepass")
driver.find_element_by_id("submitbtn").click()
After this I am am able to log in successfully,
but when I redirect to the above link using
driver.get("www.example.com/App/Details.aspx/I=5aM%+8KbCv1o=&T=M30Lr7RtcdR=&H=fRFKse5fKA==")
I get error saying please login first. Please suggest a solution for the same.

You sent the data via
password.send_keys
but after that you were to also send enter command using
password.send_keys((Keys.ENTER)
After this you have to have a
sleep(1)
or
WebDriverWait Until(expect...
before you open the next page

Related

How can I redirect the url to another url through selenium of Python?

Could I redirect the url to another url through selenium of Python?
For example, when I enter url : www.google.com in Google, it will redirect to the url : www.yahoo.com.
I know that Javascript can do it, but Python can also do it?
Now I use the below coding. I found that the url is entered, but it is refresh instead of redirect.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.common.exceptions import NoSuchElementException
PATH = C:// location of webdriver
driver = webdriver.Chrome(PATH)
url = '1'
driver.get(url)
while True:
if driver.get('1'):
print('done')
break
else:
time.sleep(1)
driver.refresh()
print('try to reload')

Handling email pop-up prompt using selenium in python

I can't figure out how to get around this in Python. The email pop-up is preventing Selenium from clicking on one of the footer links because the pop-up blocks the view of it. I ideally would like to click the "X" and not enter an email.
I've tried using what was in the Selenium documentation about prompts but none of it worked or perhaps I implemented it incorrectly. I tried some of what I already found in stack overflow, which you can see in the commented out code, but kept getting all kinds of errors.
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from urllib.request import urlopen as uReq
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
url = "https://www.standardmedia.co.ke/"
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source.encode('utf-8')
page_num = 0
##options = Firefox_options();
##options.addPreference("dom.disable_beforeunload", true)
##driver = webdriver.Firefox(options);
#click on the headings in the footer
for i in range (0,1):
footer = driver.find_elements_by_css_selector('#top-footer li')[i]
## if(driver.switch_to.alert != null):
## alert = driver.switch_to.alert
## alert.dismiss()
try:
WebDriverWait(driver, 10).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.dismiss()
print("Alert dismissed.")
except TimeoutException:
print("No alert.")
footer.click()
print("alert dismissed")
page_num += 1
subheadings = driver.find_elements_by_css_selector('.title a')
len(subheadings)
The most recent error for a Firefox web driver was "No connection could be made because the target machine actively refused it."
WebDriver Alert class is designed to work with JavaScript Alerts to wit:
Window.alert()
Window.confirm()
Window.prompt()
At the page you're trying to test this modal popup is a normal <div>
therefore you will not be able to use Alert class, you will have to normally locate close button using find_element() function and click it.
Open the page:
driver.get("https://www.standardmedia.co.ke/")
Wait for the popup to appear
popup = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, "mc-closeModal")))
Click the close button
popup.click()
Aside from the selenium connection issue, to address the main question, it looks like you are not calling the switch_to_alert function. Once you get selenium connecting, try the following:
alert = driver.switch_to_alert()
alert.dismiss()
Also note, your wait times are in seconds which seem pretty high/long in your code.

Selenium webdriver stops with no error-message when navigating through paypal login

I'm trying to log into paypal.com and make a payment automatically.
The program successfully loads the login page(https://www.paypal.com/us/signin) and inputs the email, but when I click the next button the web driver unexpectedly closes without generating an error message.
Has anyone encountered this issue before? Could it be because the next button is a disguised captcha, to keep robots from logging in?
I have already tried using time.sleep(3) to give the page time to load. I can't see any other issues with the code.
import time
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
def paypal_pay(): # pass in user address
driver = webdriver.Chrome()
timeout = 20
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "emailstuff#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
time.sleep(3)
nextElement = driver.find_element_by_id('btnNext').click()
def main():
paypal_pay()
main()
Your code is working fine but the way that you have implemented is causing the problem, I mean you are using the main() method and what it will do is, once this method gets called and executed, it will close all the connections at end. Hence the reason your browser also getting closed without any error because your code is working fine till there:
Try the below modified code without main method which works completely fine :
import time
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("chromedriver.exe");
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "hello#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
time.sleep(3)
nextElement = driver.find_element_by_id('btnNext').click()
print("=> Done...")
For more info on main(), refer this link
I hope it helps...
When I run your code After click on next button the Chrome browser crashes and in console i can see the following error.
<selenium.webdriver.remote.webelement.WebElement (session="577ff51b46a27eefeda43ccd320db48b", element="0.571535141628553-1")>
That means you need to start a RemoteWebDriver instead of ChromeDriver.
Step 1: Download the Selenium Standalone Server from following link
https://www.seleniumhq.org/download/
Step 2: open command prompt as an administrator go to the downloaded path and type the below command and press enter
java -jar selenium-server-standalone-3.141.59.jar
Step 3: To verify Hub is running, open a browser and type the below url.Default port of hub is 4444
http://localhost:4444/grid/console
Step 4: Use the following code.If you follow above steps properly it should work perfectly the below code.
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def paypal_pay(): # pass in user address
desired_caps = DesiredCapabilities.CHROME
grid_url = "http://localhost:4444/wd/hub"
driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor=grid_url)
paypal = "https://www.paypal.com/us/signin"
driver.get(paypal)
email = "emailstuff#gmail.com"
emailElement = driver.find_element_by_id('email')
print(emailElement)
emailElement.send_keys(email)
nextElement = driver.find_element_by_id('btnNext')
nextElement.click()
def main():
paypal_pay()
main()
Please let me know if this work for you.Good Luck.

Python selenium issue: able to find element but not send_keys

I am trying to login into the ESPN footytips website so that I can scrape information for one of my leagues.
I am having no issues opening an instance of Chrome and navigating to the homepage (which contains the login form) and can even select the username field but I cannot for the life of me send my login details to the form.
In debugging I know I can find and select the form submit button and the issue seems to be in passing my login details using send_keys as my exception rule always triggers after I attempt call send_keys.
Any suggestions on how to resolve would be welcomed! My script is below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
login_address = "http://www.footytips.com.au/home"
me_login = "test#test.com"
me_password = "N0TMYR3#LP#S5W0RD"
browser = webdriver.Chrome()
browser.get(login_address)
try:
login_field = browser.find_element_by_id("ft_username")
password_field = browser.find_element_by_id("ft_password")
print("User login fields found")
login_field.send_keys(me_login)
password_field.send_keys(me_password)
print("Entered login data")
submit_button = browser.find_element_by_id("signin-ft")
print("Submit button found")
submit_button.submit()
except:
print("Error: unable to enter form data")
The locators you have used doesn't uniquely identifies the login_field and the password_field. Additionally you need to wait for the respective WebElements to be visible. Here is your own code with some tweaks :
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
#lines of code
login_address = "http://www.footytips.com.au/home"
me_login = "test#test.com"
me_password = "N0TMYR3#LP#S5W0RD"
browser.get(login_address)
login_field = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='login-form']//input[#id='ft_username']")))
password_field = browser.find_element_by_xpath("//div[#class='login-form']//input[#id='ft_password']")
login_field.send_keys(me_login)
password_field.send_keys(me_password)
print("Entered login data")

selenium ERROR :policy_loader_win.cc

I encounter an ERROR when I use selenium to login a website.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome(executable_path='C:/ProgramFiles/Google/Chrome/Application/chromedriver.exe')
driver.get("http://bbs.pinggu.org/member.php?mod=logging&action=login")
login_name=driver.find_element_by_name("username")
login_name.clear()
login_name.send_keys("") ##this is login name. omit
login_name.send_keys(Keys.RETURN)
login_pass=driver.find_element_by_name("password")
login_pass.clear()
login_pass.send_keys("") ##this is login password. omit
login_pass.send_keys(Keys.RETURN)
login=driver.find_element_by_name('loginsubmit')
login.send_keys(Keys.RETURN)
driver.close()
I get an ERROR:
[9492:8552:0918/152646:ERROR:policy_loader_win.cc(449)]PReg file doesn't exist:C:\WINDOWS\System32\GroupPolicy\User\Registry.pol
I search some item, but I don't know to use it.
https://gitlab.com/fancycode/iridium-browser/blob/75a646c6596597e9c21659187fe75bbd68c2c2b9/components/policy/core/common/policy_loader_win.cc

Categories

Resources