【发布时间】:2015-01-12 20:00:31
【问题描述】:
我正在尝试存储由邮政编码查找生成的地址值,然后创建一个列表,我可以使用 python Random 模块使用 random.choice 选择随机值
场景:
输入邮政编码,点击“搜索” - 下拉列表会动态填充可用选项。
我正在使用字典将表单值存储为 xpath,然后使用 webdriver 到 find_elements_by_xpath 或 find_element_by_xpath。
代码看起来像这样(格式不正确,仅供参考):
__author__ = 'scott'
from selenium import webdriver
import random
driver = webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.somerandomsite')
formFields = {'postcode' : "//INPUT[@id='postcode']",
'county' : "//SELECT[@id='address']/option"}
pcList = ['BD23 1DN', 'BD20 0JZ']
#picks a random postcode from pcList#
driver.find_element_by_xpath(formFields['postcode']).send_keys(random.choice(pcList))
driver.find_elements_by_xpath(formFields['county'])
#####now need to store the values from county and select a random option from the list######
driver.close()
在我的邮政编码上使用随机模块不是问题。
如果有人可以提供一些指导或指出我可以参考的方向,我将不胜感激 - 我只是 Selenium 和 Python 的菜鸟 - 取得了稳步进展,但似乎在这个问题上绕圈子- 我的第一个问题是使用 find_element_by_xpath 一个简单的 's' 缺少 'element' 让我有一段时间了。
【问题讨论】:
标签: python selenium xpath selenium-webdriver webdriver