Python/Selenium/PhantomJS-执行之间保留的数据 [英] Python/Selenium/PhantomJS - Data retained between execution

查看:173
本文介绍了Python/Selenium/PhantomJS-执行之间保留的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习硒.我正在使用Python 2.7. Phantom JS-2.1.1.

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

其他详细信息 如您在电子邮件"框中所看到的,保留了最后的执行数据.在复选框"中,我单击了相同的复选框,然后未选中它.至于名称字段-我使用clear()方法,并且较早的数据已清除.电子邮件文本框无法使用相同的方法.

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.

请找到python代码段-

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()

编辑2-其他信息

  1. 该站点是使用ReactJS创建的.由于我在React中为零,所以我不知道它是如何工作的.我考虑过更改HTML输入属性的值,但是在检查时发现它始终是正确的
  2. 我认为问题不在于复选框,我认为这是我执行Python和Selenium时遇到的问题,因为数据保存在页面和脚本执行之间
  3. 不确定,这一点是否重要-我正在c9.io中进行开发

图片-点击之前

图片-点击后

推荐答案

该站点可能会将用户输入缓存在Cookie或本地存储中.网站通常会执行此操作,以使您可以在页面之间来回导航,或稍后返回到表单,而无需再次填写所有详细信息.例如,这是网站如何在React中保持状态的方法.

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.

您可以使用浏览器的开发工具进行查找.例如,在Chrome浏览器中的操作方法如下.该图显示了可能正在使用的各种类型的存储.

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.

如果您想在没有任何先前输入的情况下开始每个测试,则需要将其删除.如果该站点存储cookie,并且您没有要保存的其他cookie,则可以删除其中的全部:

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()

还可以删除单个Cookie .

如果该站点使用本地存储,那么目前使用python有点麻烦,因为它看起来不像python绑定实现了访问本地存储的方法,因此

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();')

这将删除与当前域关联的所有本地存储.与Cookie一样,有一些访问单个项目的方法 ,如有必要.

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.

这篇关于Python/Selenium/PhantomJS-执行之间保留的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆