gpt4 book ai didi

python - Selenium可以自动检测网页上的文本框并输入值吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:28:54 25 4
gpt4 key购买 nike

我是测试自动化的新手。通过学习一些基本的selenium,我知道我可以使用selenium工具在线填写一些表格。下面是我使用的一些代码供您引用:

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")

这是网络截图:

enter image description here

我的问题是,是否可以自动查找文本框并填写一些随机值。我只是测试是否可以创建新记录,因此值并不重要。

谢谢

最佳答案

由于所需的元素位于 <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函数将以更好的方式满足您的需求,但这应该超出这个问题的范围

关于python - Selenium可以自动检测网页上的文本框并输入值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53663114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com