Python/Selenium/PhantomJS - Data retained between

2019-09-14 10:47发布

问题:

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

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

image - after click

回答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.

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.