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:
Related
I am new to Selenium and I am trying to mimic user actions on a site to download a csv file.
The "Get Data" button generates the link "download file in csv format" when I do it manually but it doesn't work with the automation.
I get an exception saying the link was not found.
Here are the things that i tried:
Adding explicit waits to have enough time for the page to load. Didn't work.
Adding explicit waits between click on get_data and before clicking on download link. Didn't work.
Tried different ways of the clicking the "get data" button ie
i) get element by xpath and then click.
ii) driver.wait_until(.... driver.find_element.. and not(#disabled). to make sure the button is not disabled. didn't work
iii) Added asserts to make sure the button is displayed and enabled.
** Here is anomaly which I don't understand.
I went through the html file to understand how its working and there is javascript which validates the input data. So I guess the "get_data" wont work if the input data is incorrect.
So I tried to display the text after entering the symbol "SBIN" via sendkeys by
print(driver.find_element_by_id("symbol").text)
and this prints empty. Not sure if this is problem.
Would be really grateful for a solution as it would save me ton of manual effort is getting stock data.
Here is the video with the changes suggested by KunduK
youtube.com/watch?v=hBjjfPIeSCQ&feature=youtu.be
Here is the website.
https://www1.nseindia.com/products/content/equities/equities/eq_security.htm
Here is the python script for the webdriver.
# -*- 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 Test1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.verificationErrors = []
self.accept_next_alert = True
def test_1(self):
driver = self.driver
driver.get("https://www1.nseindia.com/products/content/equities/equities/eq_security.htm")
driver.find_element_by_id("dataType").click()
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Get historical data for:'])[1]/following::option[3]").click()
driver.find_element_by_id("symbol").click()
driver.find_element_by_id("symbol").clear()
driver.find_element_by_id("symbol").send_keys("SBIN")
driver.find_element_by_id("series").click()
Select(driver.find_element_by_id("series")).select_by_visible_text("EQ")
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Select series :'])[1]/following::option[15]").click()
Select(driver.find_element_by_id("dateRange")).select_by_visible_text("24 Months")
driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='For past:'])[1]/following::option[8]").click()
driver.find_element_by_id("get").click()
driver.find_element_by_link_text("Download file in csv format").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()
Try to change this line:
driver.find_element_by_link_text("Download file in csv format").click()
to below code:
Wait element to be clickable.
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
wait = WebDriverWait(driver, 10) //Explicit Waits to wait for ExpectedCondition
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Download file in csv format'))).click()
Induce WebDriverWait() and element_to_be_clickable() and following xpath to click on csv file link.
secondly to get the value from input element you have to use element.get_attribute('value')
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.support.select import Select
driver = webdriver.Chrome()
driver.get("https://www1.nseindia.com/products/content/equities/equities/eq_security.htm")
driver.find_element_by_id("dataType").click()
driver.find_element_by_xpath(
"(.//*[normalize-space(text()) and normalize-space(.)='Get historical data for:'])[1]/following::option[3]").click()
driver.find_element_by_id("symbol").click()
driver.find_element_by_id("symbol").clear()
driver.find_element_by_id("symbol").send_keys("SBIN")
# To get the value from input text
print(WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input#symbol'))).get_attribute(
'value'))
driver.find_element_by_id("series").click()
Select(driver.find_element_by_id("series")).select_by_visible_text("EQ")
driver.find_element_by_xpath(
"(.//*[normalize-space(text()) and normalize-space(.)='Select series :'])[1]/following::option[15]").click()
Select(driver.find_element_by_id("dateRange")).select_by_visible_text("24 Months")
driver.find_element_by_xpath(
"(.//*[normalize-space(text()) and normalize-space(.)='For past:'])[1]/following::option[8]").click()
# driver.find_element_by_id("get").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "get"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Download file in csv format']"))).click()
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()
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
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
"""
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