簡體   English   中英

硒可以自動檢測網頁上的文本框並輸入值嗎?

[英]Can selenium automatically detect text box on webpage and input values?

我是自動化測試的新手。 通過學習一些基本的硒,我知道我可以使用硒工具在線填寫一些表格。 以下是一些我供您參考的代碼:

elem = self.driver.find_element_by_xpath("//*[@title='Registration']")
elem.click()
iframe = self.driver.find_elements_by_id('frm_reg_load_patient')[0]
self.driver.switch_to.default_content()
self.driver.switch_to.frame(iframe)
elem = self.driver.find_element_by_id("txtPatientLastName")
elem.clear()
elem.send_keys("Peng")
elem = self.driver.find_element_by_id("txtPatientFirstName")
elem.clear()
elem.send_keys("Richard")

這是網絡的屏幕截圖:

在此處輸入圖片說明

我的問題是,是否有可能自動找到文本框並填寫一些隨機值。 我只是在測試是否可以創建新記錄,因此這些值並不重要。

謝謝

由於所需的元素在<iframe>因此您必須:

  • 誘導WebDriverWait獲得所需的幀並切換到該幀
  • 誘導WebDriverWait使所需的元素可單擊
  • 您可以使用以下解決方案:

     WebDriverWait(self.driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"frm_reg_load_patient"))) WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, "txtPatientLastName"))).send_keys("Peng") self.driver.find_element_by_id("txtPatientFirstName").send_keys("Richard") 
  • 注意 :您必須添加以下導入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC 

更新資料

根據您的注釋更新,您的腳本/程序將更有效WebDriverWait代替隱式等待 ,但是老實說,沒有可用的現成的function()掃掠頁面以查找任何元素填充隨機數據 寫一個新wrapper功能將滿足您的需求,以更好的方式,但應該是超出范圍了這個問題。

我自己找到了答案。 如果有人有同樣的問題:

//find all input fields where type = text or password and store them In array list 
txtfields.
 List<WebElement> txtfields = driver.findElements(By.xpath("//input[@type='text' or 
 @type='password']"));

//for loop to send text In all text box one by one.
 for(int a=0; a<txtfields.size();a++){   
 txtfields.get(a).sendKeys("Text"+(a+1));  
 }
 Thread.sleep(3000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM