-1

我在 selenium ide 上做了一个记录并将其导出到 python webdriver,我想存储一个文本字段的值,但我无法让它工作..

除了这部分之外,该脚本几乎是标准的:

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




class Shorturl(unittest.TestCase):
def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(30)
    self.base_url = "http://shorturl.com/"
    self.verificationErrors = []
    self.accept_next_alert = True

def test_shorturl(self):
    driver = self.driver
    driver.get(self.base_url + "/")
    driver.find_element_by_name("longurl").clear()
    driver.find_element_by_name("longurl").send_keys("http://lalala.com/")
    driver.find_element_by_css_selector("input[type=\"image\"]").click()

    driver.find_element_by_css_selector("input[type=\"submit\"]").click()
    driver.find_element_by_id("txtfld").click()
    time.sleep(3)

    noah = driver.find_element_by_id("txtfld").text
    print noah

    file = open("datafile.txt", "w")
    file.write(noah)
    file.close()

def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e: return False
    return True

def is_alert_present(self):
    try: self.driver.switch_to_alert()
    except NoAlertPresentException, e: return False
    return True

def close_alert_and_get_its_text(self):
    try:
        alert = self.driver.switch_to_alert()
        alert_text = alert.text
        if self.accept_next_alert:
            alert.accept()
        else:
            alert.dismiss()
        return alert_text
    finally: self.accept_next_alert = True

def tearDown(self):
    #self.driver.quit()
    self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
unittest.main()

它单击它,但它不打印它或将其保存在文件中。

谢谢你。

4

1 回答 1

0

如果您想知道它设置为什么值,通常使用“文本字段”,您将需要获取该值而不是其文本。

driver.find_element_by_id("txtfld").get_attribute("value")
于 2013-08-28T09:36:52.083 回答