Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python bindings, Selenium WebDriver click() is not working sometimes.

Tags:

I am trying to submit an input(type= button).But I am unable to update the value. Any help is appreciated. I have attached the testcase below for your reference.

search for CLICK FAILS HERE

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException import unittest, time, re,datetime,os,sys  from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains   def is_element_present(inst,selector,value):     try:         inst.find_element(by=selector, value=value)         return True     except:         return False    class Testing(unittest.TestCase):     def setUp(self):         self.driver = webdriver.Chrome()         self.driver.implicitly_wait(5)         self.base_url = "http://new.ossmoketest.appspot.com/"         self.verificationErrors = []      def test_ing(self):         try:              driver = self.driver             driver.get(self.base_url + "/Apab4b39d4_09d7_11e1_8df9_139372201eeb/1/signin?forward=/%3F")             now = datetime.datetime.now()             start = time.clock()              for i in range(5000000):                 try:                     if is_element_present(driver,By.ID,"userid"): break                 except: pass             else: self.fail("time out")             end = time.clock()             diff = end - start             print diff             driver.find_element_by_id("userid").clear()             driver.find_element_by_id("userid").send_keys("[email protected]")             driver.find_element_by_xpath("//input[@src='/static/images/signup.png']").click()             print 'finished'             start = time.clock()              for i in range(5000000):                 try:                     if is_element_present(driver,By.LINK_TEXT,"Logout"): break                 except: pass             else: self.fail("time out")             end = time.clock()             diff = end - start             print diff             time.sleep(5)              start = time.clock()             name = "smoketest"+ str(now.minute) +str(now.second)             for i in range(5000000):                 try:                     if is_element_present(driver,By.LINK_TEXT,"PurchaseOrder"): break                 except: pass             else: self.fail("time out")             end = time.clock()             diff = end - start             driver.find_element_by_link_text('PurchaseOrder').click()             name = "smoketest"+ str(now.minute) +str(now.second)             start = time.clock()             for i in range(5000000):                 try:                     if is_element_present(driver,By.ID,"Customer_Name"): break                 except: pass             else: self.fail("time out")             end = time.clock()             diff = end - start              newproduct = "rexona"+ str(now.minute) +str(now.second)             newprice = str(now.minute) +str(now.second)             newprice = float(newprice)             print newprice             driver.find_element_by_xpath("//input[starts-with(@id,'New_Product')]").send_keys(newproduct)             driver.find_element_by_xpath("//input[starts-with(@id,'Price')]").clear()             time.sleep(3)              driver.find_element_by_xpath("//input[starts-with(@id,'Price')]").send_keys(Keys.CONTROL+'a'+Keys.NULL, str(newprice))             Mouse_cntrl = ActionChains(driver)             Mouse_cntrl.release(driver.find_element_by_xpath("//input[starts-with(@id,'Price')]"))             value = newprice             print value             time.sleep(2)             print 'start'             print driver.find_element_by_xpath("//input[starts-with(@id,'NewItem_NewItem')]").get_attribute('data-id')             # ------------------------CLICK FAILS HERE ------------------------------ #            driver.find_element_by_xpath("//input[starts-with(@id,'NewItem_NewItem')]").click() #            driver.find_element_by_xpath("//input[starts-with(@id,'NewItem_NewItem')]").submit()             driver.find_element_by_xpath("//input[starts-with(@id,'NewItem_NewItem')]").send_keys(keys.ENTER) #            Mouse_cntrl.double_click(driver.find_element_by_xpath("//input[starts-with(@id,'NewItem_NewItem')]"))             for i in range(10):                 try:                     print driver.switch_to_alert().text                     if driver.switch_to_alert():                         driver.switch_to_alert().accept()                         break                 except: pass                 time.sleep(1)             else:                 print "alert not found"             print 'finished  -- '             time.sleep(8)             driver.find_element_by_xpath("//input[starts-with(@id,'Product')]").click()             arg = newproduct             print 'end'             for i in range(60):                 try:                     if is_element_present(driver,By.LINK_TEXT,arg): break                 except: pass                 time.sleep(1)             else: self.fail("time out")     #        sel.mouse_over("//html/body/ul/li/a[.=\""+arg+"\"]")             driver.find_element_by_link_text(arg).click()             start = time.clock()             time.sleep(25)             for i in range(1000000):                 try:                      if newprice == float(driver.find_element_by_id('Unit_Price').text):                         end = time.clock()                         diff = end - start                         log.log(module='Smoke',testcase='Action New', result='Pass',time_taken= diff)                         break                 except: pass             else:                 log.log(module='Smoke',testcase='Action New', result='Fail')                 self.fail('New Failure')             log.log(module='Smoke',testcase='On Submit', result='Pass',time_taken= diff)             driver.find_element_by_id('Quantity').send_keys(Keys.CONTROL+'a'+Keys.NULL,"1")             time.sleep(5)             start = time.clock()             for i in range(1000000):                 try:                     if value == float(driver.find_element_by_id('Unit_Price').text):                         end = time.clock()                         diff = end - start                         log.log(module='Smoke',testcase='Multiply', result='Pass',time_taken= diff)                         break                 except: pass             else: self.fail("time out")             for i in range(1000000):                 try:                     if value == float(driver.find_element_by_id('Amount').text):                         end = time.clock()                         diff = end - start                         log.log(module='Smoke',testcase='DSUM with Parent', result='Pass',time_taken= diff)                         break                 except: pass             else:                 end = time.clock()                 diff = end - start                 log.log(module='Smoke',testcase='DSUM with Parent', result='Fail',time_taken= diff)                 self.fail("time out")          except:             self.driver.quit()             e = sys.exc_info()[1]             print str(e)      def tearDown(self):         self.driver.quit()         self.assertEqual([], self.verificationErrors)  if __name__ == "__main__":      unittest.main() 

It has been a showstopper for my work. Any help is appreciated.Thanks

like image 468
senthil3569 Avatar asked Jan 12 '12 09:01

senthil3569


People also ask

Why click is not working in Selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

How does click operation work in Selenium?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

Is Selenium clickable in Python?

We can check if the element is clickable or not in Selenium webdriver using synchronization. In synchronization, there is an explicit wait where the driver waits till an expected condition for an element is met. To verify, if the element can be clicked, we shall use the elementToBeClickable condition.

How do you click and drag in Selenium Python?

In our case, we'll use these operations for drag and drop in Selenium Python by simulating click using click_and_hold, then dragging by using move_to_element or move_by_offset or combo, and by finally releasing, i.e., dropping the selected element.


1 Answers

You could try substituting .click() with .send_keys("\n"), which is equivalent to "Pressing enter while focusing on an element".

So this:

driver.find_element_by_link_text('PurchaseOrder').click() 

would become this:

driver.find_element_by_link_text('PurchaseOrder').send_keys("\n") 
like image 109
EwyynTomato Avatar answered Sep 19 '22 12:09

EwyynTomato