【问题标题】:Using Python (Selenium) to read, save, and then input a value from Web Page [closed]使用Python(Selenium)读取,保存,然后从网页输入一个值[关闭]
【发布时间】:2013-08-22 17:35:21
【问题描述】:

我们对使用 Python 和 Selenium 还是很陌生。所以请多多包涵。作为我们网站测试自动化工作的一部分,我们使用了一个帮助类来显示在新客户注册期间使用的验证码(效果很好)。我们现在尝试读取该值,将其保存到内存(字符串),然后将该保存的值输入到正确的页面元素中。这对我们来说是全新的,我们不知所措。这是我们目前所拥有的。

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
import urllib, urllib2

class NewAccountTests(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(5)
        self.base_url = "http://www.test.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
        response = urllib.urlopen("http://www.test.com/")
        htmlSource = sock.read(id = "captcha")
        var.captcha = "htmlSource"
        sock.close()

#SIGN UP NEW USER

    def test_00_sign_up(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("name").send_keys("Foo")
        driver.find_element_by_id("email").send_keys("test@me.com")
        driver.find_element_by_id("screenname").send_keys("1234")
        driver.find_element_by_id("password").send_keys("xxx")
        driver.find_element_by_id("password2").send_keys("xxx")
        driver.find_element_by_id("option1").click()
        driver.find_element_by_id("option2").click()
        driver.find_element_by_id("captcha").click()
        >> I don't how to send the var string to the element "captcha"
        driver.find_element_by_id("registration_button").click()

我确信有人很容易知道这一点,因此我们将不胜感激。

【问题讨论】:

  • @Marcin 它隐藏在代码中 :)
  • 您到底想向“验证码”元素发送什么?它是一个变量吗?从哪里来?
  • 我正在尝试了解如何发送var.captcha = "htmlSource"。是的,来自def setup(self)。我不认为只使用.send_keys 会在这里工作。
  • 应该可以 :) 如果没有,请告诉我们!
  • 不。我得到:TypeError: read() got an unexpected keyword argument 'id' 看看参数,这应该可以吗?

标签: python selenium


【解决方案1】:

我认为你需要使用send_keys():

send_keys(*value)

模拟输入元素。

driver.find_element_by_id("captcha").send_keys("test")

【讨论】:

  • 真的吗?那么所有其余的代码都是准确的吗?如果我这样做,它只会将 T E S T 发送到页面元素。
  • @Marcin 你非常乐于助人。请去“帮助”别人。
猜你喜欢
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
  • 2018-01-01
相关资源
最近更新 更多