0

I am trying to learn Selenium. I am using Python 2.7. Phantom JS - 2.1.1.

Background - The script is trying to enter data into the controls. The script is able to catch hold of controls. However the data from older execution is retained.

SCREENSHOT

enter image description here

ADDITIONAL DETAILS As you can see in the EMAIL box, the last execution data is retained. In Checkboxes I clicked the same checkbox and then it appears unselected. As for Name field - I used clear() method and the earlier data was cleared. Same method is not working for email text box.

Please find the python code snippet -

import time,traceback
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

COMPANY_NAME = 'AHXJ OF KCH'
FIRST_NAME = 'Bill'
LAST_NAME = 'CLINTON'
EMAIL = 'bill.clinton@whitehouse.com'

driver = webdriver.PhantomJS()
driver.delete_all_cookies()
driver.implicitly_wait(10)
driver.set_window_size(1120, 550)
try:
    driver.get("https://username:password@url/")
    select_state = Select(driver.find_element_by_id('state_abbrev'))
    select_state.select_by_visible_text('Arizona')
    time.sleep(5)
    select_business_segment =           Select(driver.find_element_by_id('business_segment_id'))
    select_business_segment.select_by_visible_text('IT/Technology')
    time.sleep(5)
    select_business_type = Select(driver.find_element_by_id('business_type_id'))
    select_business_type.select_by_visible_text('Application Development')
    driver.save_screenshot(COMPANY_NAME+ '_home_page_screenshot.png')
    driver.find_element_by_xpath('//*[@id="chubb_commercial_entry_form"]/div/button').click()
    time.sleep(10)
    #wait = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'product_codes__bop')))
    driver.find_element_by_xpath('//*[@id="field_for_product_codes__bop"]/label').click()
    comp_name = driver.find_element_by_id('business_name')
    comp_name.clear()
    comp_name.send_keys(COMPANY_NAME)
    email = driver.find_element_by_id('email')
    email.clear()
    email.send_keys(EMAIL)
    driver.save_screenshot(COMPANY_NAME+ '_business_info_screenshot.png')
    driver.find_element_by_xpath('//*[@id="commercial-app"]/div/div[2]/div[2]/div/div[2]/form/div[1]/div/div/button').click()
    time.sleep(10)
   ...
except Exception,e:
    print e
    driver.save_screenshot('error_screenshot.png')
    traceback.print_exc()
finally:
    driver.quit()

EDIT 2 - ADDITIONAL INFORMATION

  1. The site is made using ReactJS. Since I am zero in React, I have no idea how it works. I thought of changing the value of HTML input attribute, but on inspecting I found that it is always true
  2. I dont think the problem is with checkbox, I feel it is the problem the way I am executing Python and Selenium because Data is saved across page and across script execution
  3. Not sure, if this point is important - I am developing in c9.io

image - before click enter image description here

image - after click enter image description here

Tushar Saurabh
  • 687
  • 2
  • 13
  • 27
  • 1
    It might be helpful, for you and for us, if you include only the code necessary to reproduce your issue. E.g., delete everything after the email bit, and remove everything before that that isn't necessary to reproduce the behaviour you want to correct. – Mark Lapierre Apr 19 '17 at 17:06
  • One of these answers may help you: http://stackoverflow.com/questions/3249583/selenium-webdriver-i-want-to-overwrite-value-in-field-instead-of-appending-to-i – Mark Lapierre Apr 19 '17 at 17:21
  • @MarkLapierre, thanks for the link. It solved the problem related to text field. However it wont solve all the problem, The value in checkbox is still retained. – Tushar Saurabh Apr 20 '17 at 06:42
  • It's not really clear what's happening. Can you change the checkbox value? If so I suggest checking it if you want it checked, or uncheck it if you want it unchecked. If that doesn't help, can you tell me *exactly* what is happening with the checkbox, and what you want to happen? – Mark Lapierre Apr 20 '17 at 16:17
  • @MarkLapierre, I have provided additional detail in under "EDIT 2 - ADDITIONAL INFORMATION" in original post. First Pass: I enter data in all the different pages after loggin in. Second Pass - The data I entered in Pass 1 is retained. Hence I have to cleanup data before I enter fresh data.Text Box can be cleaned, Drop down doesnt pose a problem. But Checkbox and Radio buttons pose a problem. If Yes was clicked in First Pass, then in second pass it is retained, which I dont want – Tushar Saurabh Apr 21 '17 at 06:45
  • @MarkLapierre, I am executing the script document.getElementById('id').checked = true/false to check uncheck. My issue related to checkbox is solved. However I still have a question "Why data is persisting across execution" – Tushar Saurabh Apr 21 '17 at 08:36
  • It might be the site itself. It probably stores the data in your browser (cookie or more likely local storage) so that you can navigate back and forth between pages and not lose everything. For example: http://blog.mgechev.com/2015/03/05/persistent-state-reactjs/ – Mark Lapierre Apr 21 '17 at 12:38

1 Answers1

1

The site is probably caching user input in cookies or in local storage. Sites will typically do this to allow you to navigate back and forth between pages, or to return to a form later without having to fill in all the details again. For example, this is how a site might persist state in React.

You can use your browser's dev tools to find out. For example, here's how you'd do that in Chrome. The image shows the various types of storage that might be in use.

Chrome dev tools local storage inspection

If you want to start each test without any previous input you'll need to delete it. If the site stores cookies and you don't have any other cookies you want to save, you can delete all of them:

driver.delete_all_cookies()

It's also possible to delete individual cookies.

If the site uses local storage it's a little tricker with python, for the moment, because it doesn't look like the python bindings implement a means to access local storage, like java does. I could be wrong. But you can use javascript, like so:

driver.execute_script('window.localStorage.clear();')

That will delete all of the local storage associated with the current domain. As with cookies there are ways to access individual items, if necessary.

Mark Lapierre
  • 1,067
  • 9
  • 15