-1
的價值我已經對硒IDE的記錄,並遠銷它webdriver的蟒蛇,我想存儲一個文本框的值,但我不能讓它的工作..蟒蛇硒保存textfileld
腳本是相當很多標準除了這部分:
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()
它點擊它,但它不打印它或保存在文件中。
謝謝。
您能否注意到您嘗試解析的實際url? – alecxe
通常,如果您想知道它設置了哪個「值」,那麼通常需要使用「文本字段」來獲取該值,而不是其「文本」。 'driver.find_element_by_id(「txtfld」)。get_attribute(「value」)' –
明白了,我怎麼能給你一個最好的答案或者什麼,非常感謝你的幫助,過上了美好的生活。 – user215379