Selenium Python click on a log in with Facebook image button - python

I want to use selenium in python to automate using facebook to log in to a site.
I have managed to locate the image using xpath but nothing happens when click() is called. Here is my line of code:
driver.find_element_by_xpath('//a[img/#src="http://kikadesigns.co.za/wp-content/uploads/2013/11/fb.jpg"]').click()
The site I'm trying to run the script on: http://rr2.rr.desds.com/site/login which contains the log in with Facebook image.
My full script:
# -*- 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
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class FBTest2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://rr2.rr.desds.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_f_b_test2(self):
driver = self.driver
driver.get(self.base_url + "/site/login")
#driver.find_element_by_css_selector("td > a > img").click()
WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.XPATH, "//img[contains(#src,'fb.jpg')]"))
)
driver.find_element_by_xpath("//img[contains(#src,'fb.jpg')]").click()
WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.ID, "u_0_1"))
)
driver.find_element_by_id("u_0_1").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
# def tearDown(self):
# self.driver.quit()
# self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()

The double quotes are misplaced I guess. Try the below code instead:
driver.find_element_by_xpath('//img[contains(#src,"fb.jpg")]').click()
It works when modified and tried with Java.

Try this
enter = driver.find_element_by_xpath("//img[contains(#src,'fb.jpg')]")
enter.submit()
and make sure image on which you click points to some location

Related

Python Selenium rel=nofollow link

The Python Selenium code
driver.find_element_by_link_text("Off").click()
is correct and executes without errors. The fault is that the click follows the "nofollow" to the 404 error. This code should only change the view state of the webpage. This behaviour is true for both Firefox and Chrome webdriver. The only other changes to my development environment were the Linux kernel updates.
I would appreciate any suggestions.
A FULL SAMPLE BELOW:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class UntitledTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.katalon.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_untitled_test_case(self):
driver = self.driver
driver.get("https://www.modelmayhem.com/portfolio/768765/viewall")
driver.find_element_by_link_text("Off").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
def links_processor(self, links):
# A hook into the links processing from an existing page, done in order to not follow "nofollow" links
ret_links = list()
if links:
for link in links:
if not link.nofollow: ret_links.append(link)
return ret_links
if __name__ == "__main__":
unittest.main()
Problem is cookies popup. With implicitly_wait just add click to "I AGREE" button:
driver.find_element_by_link_text("I AGREE").click()
driver.find_element_by_link_text("Off").click()
If you'll use WebDriverWait, to wait until "I AGREE" will be clickable and close it:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "I AGREE"))).click()
driver.find_element_by_link_text("Off").click()
Screenshot of cookies popup:

Selenium Webdriver - Python - leboncoin - pb to select a button with an accent

I am trying to fill in a form automatically on the following website: 'https://www.leboncoin.fr/'
I have recorded a script with Selenium IDE.
I have a function to connect automatically by clicking on the button 'Se connecter' and filling my pwd and username. It works fine
I have set up specific credential for this topic
email: thecoingood#gmail.com
pwd: thecoingood1
the code is
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Connectionwebdriver2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Safari()
self.driver.implicitly_wait(30)
self.driver.maximize_window()
self.base_urldr = "https://compteperso.leboncoin.fr/account/index.html"
self.verificationErrors = []
self.accept_next_alert = True
def test_connectionwebdriver2(self):
driver = self.driver
driver.get(self.base_urldr)
driver.find_element_by_name("st_username").clear()
driver.find_element_by_name("st_username").send_keys("thecoingood#gmail.com ")
driver.find_element_by_name("st_passwd").clear()
driver.find_element_by_name("st_passwd").send_keys("thecoingood1")
driver.find_element_by_id("connect_button").click()
#driver.get("https://www2.leboncoin.fr/ai?ca=15_s")
my_annonce = WebDriverWait(self.driver, 10)\
.until(EC.element_to_be_clickable((By.LINK_TEXT, "Supprimer")))
my_annonce.click()
#time.sleep(10)
#driver.find_element_by_link_text("Supprimer").click()
#WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#href='//https://compteperso.leboncoin.fr/account/index.html?ca=12_s' and contains(.,'posez une annonce')]"))).click()
#Select(driver.find_element_by_id("category")).select_by_visible_text('Locations')
#Select(driver.find_element_by_id('cat10')).select()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main(verbosity=2)
Once connected, I am redirected to
https://compteperso.leboncoin.fr/account/index.html?ca=12_s
(question: is the object used in selenium updated with this new address or still sticks with the initial one which may create the issue)
when I try to click on the
Déposez une annonce
with this code
driver.find_element_by_link_text(u"Déposez une annonce").click()
Nothing happens (no error).
I believe this is related to the fact the link is not yet visible.
I have tried to add time.sleep() and also read
How can I get Selenium Web Driver to wait for an element to be accessible, not just present?
but coud not resolve this. I could add a direct link to the page, but I would like to understand.
Thanks in advance
As per your question to click on the tab / link with text as Déposez une annonce you can use the following line of code :
To click the TAB with text as Déposez une annonce use :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='deposer']/a[contains(#href,'//www2.leboncoin.fr/ai?ca=') and contains(.,'une annonce')]"))).click()
To click the BUTTON with text as Déposez une annonce use :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='create']/a[contains(#href,'//www2.leboncoin.fr/ai?ca=') and contains(.,'une annonce')]"))).click()

How to Get value from ini file with Python for selenium test?

After java i'm trying to create a simple login test with selenium using python.
So in my project i have created test.ini which contains:
[login]
username = tester#myapp.com
my python test file is:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
from web import*
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome("C://Selenium-driver//chromedriver.exe")
self.driver.implicitly_wait(30)
self.base_url = "http://myapp.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_x(self):
driver = self.driver
driver.get(self.base_url + "/")
timestr = time.strftime('%Y%m%d-%H%M%S')
time.sleep(30)
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser # ver. < 3.0
# instantiate
config = ConfigParser()
# parse existing file
config.read('test.ini')
# read values from a section
string_val = config.get('login', 'username')
driver.find_element_by_xpath("(//input[#id='input'])[2]").click()
driver.find_element_by_xpath("(//input[#id='input'])[2]").clear()
driver.find_element_by_xpath("(//input[#id='input'])[2]").send_keys(string_val)
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
So when i run the python test, the username is never used from ini file.
the browser start with the url and then it closes by itself without errors.
Why i use ini file? because i wanted to create something like a class repository for web element and it did not work for me.
So after searching in google, it looks that i can use ini file to store all web element and then use in my test.
Can anyone help me, please.
Thank you

Load Firefox with python selenium script, but it will open the skype support page

I recorded a script by selenium IDE in Firefox, and exported to python webdriver. but when I run the code, it will open the skype support page at the same time. Don't know why.
My firefox is the latest(50.1.0), python 2.7.12, selenium 3.0.2
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Test2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.cic.gc.ca/english/visit/"
self.verificationErrors = []
self.accept_next_alert = True
def test_2(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").click()
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("tt")
driver.find_element_by_id("su").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
# def tearDown(self):
# self.driver.quit()
# self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
I am not a python/selenium expert, but I think I have experienced the same problem. I have just recently installed selenium in my Enthought Canopy application.The following simple code produces a Skype web-browser window:
#file: selenium2.py .
from selenium import webdriver
#browser = webdriver.Chrome()
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/') # always gets skype ???
"""
This url window is the response:
https://support.skype.com/en/faq/FA34612/what-is-the-skype-extension
"""

run a based selenium script from my web hosting

I have a python script generated from selenium, it works fine on my localhost.
Now I want to run it from my web hosting, I already checked that my web hosting support python.
If not possible, is there an alternative solution for selenium?
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class HellowWorld(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_hellow_world(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("hello world")
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
I found a good alternative based on google's puppeteer library.
All information that do you need is in this web page pypeteer
You can install it using: pip install pyppeteer
Here an example of code to take a screenshoot:
import asyncio
from pyppeteer import launch
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('https://example.com')
await page.screenshot({'path': 'example.png'})
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
I think that the best option is to connect to a remote webdriver setting a remote host with selenium port.
Example: http://remotehost:4444
const {Builder} = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
let options = new firefox.Options()
.setProfile('/profile/path/on/remote/host')
.setBinary('/install/dir/on/remote/host/firefox-bin');
let driver = new Builder()
.forBrowser('firefox')
.usingServer('http://{remote-host}:4444/wd/hub')
.setFirefoxOptions(options)
.build();

Categories