python+selenium简单的学习例子

这篇博客介绍了Python结合Selenium进行网页自动化测试的基础知识,包括通过ID和XPath定位元素,利用send_keys发送键盘输入,以及如何处理元素加载延迟的问题,特别是详细探讨了XPath的定位方法和等待策略。
摘要由CSDN通过智能技术生成

键盘输入常规操作。

以下的范例包含:
1.python 元素定位 by.id,by.xpath(如果想要明白怎么使用xpath定位可以先学习xpath https://www.runoob.com/xpath/xpath-tutorial.html)
2.python 的键盘操作 sendkey
3.等待等(有时候页面中报错说元素定位不到,但是排除定位路径错误之后,可能就是等待时延不够,需要设置等待,三种等待方式),

class Creatpr():
    def __init__(self,driver,vendornumber,itemnumber,qty):
        self.driver =driver
        self.vendornumber = vendornumber
        self.itemnumber = itemnumber
        self.qty = qty

    def Creat_Pr(self):
        drive= self.driver
        vendornumber = self.vendornumber
        itemnumber = self.itemnumber
        qty = self.qty
        drive.get("xxx")
        drive.implicitly_wait(20)
        drive.find_element_by_id("UserName").send_keys("xxxx")
        drive.find_element_by_id("Password").send_keys(u'xxxx')
        drive.find_element_by_id("sign_button").send_keys(Keys.ENTER)
        drive.find_element_by_link_text("with AD").click()
        wait = WebDriverWait(drive, 70)
        wait.until(EC.presence_of_element_located((By.XPATH,"//*[@id='nc3-framework-mainview']/pr-form/div/div[1]/vendor-input/po-section/div/div[2]/form/div[1]/div/input"))).send_keys(vendornumber)
        drive.find_element_by_xpath("//div[@class='icon-container ng-star-inserted']/span").click()
        WebDriverWait(drive, 70, 0.5).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='itemNumberInput']"))).send_keys(itemnumber)
        drive.find_element_by_xpath("//input[@id='itemNumberInput']").send_keys(Keys.ENTER)
        WebDriverWait(drive, 100, 0.5).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='ng-touched ng-dirty ng-star-inserted ng-valid']/kendo-numerictextbox[@formcontrolname='wh07Qty']/span/input"))).send_keys(qty)
        drive.find_element_by_xpath("//div[@class='eta-picker']/kendo-datepicker/span/kendo-dateinput/span/input").send_keys( Keys.BACK_SPACE, Keys.BACK_SPACE, "10", "11", "2020")
        drive.find_element_by_xpath("//div[@class='nk-function-zone']/div[@class='nk-function-item']/button").click()
        WebDriverWait(drive, 200, 0.5).until(EC.element_to_be_clickable((By.XPATH, "//pr-notification-center-dialog/kendo-dialog/div[2]/kendo-dialog-actions/button[1]"))).click()
        drive.find_element_by_xpath("//div[@class='function-panel']/div[@class='nk-function-zone']/div[1]/button").click()

        WebDriverWait(drive, 1500, 1).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//kendo-tabstrip/ul[@role='tablist']/li[2]/span"))).click()
        ponumber=drive.find_element_by_xpath("//div[@class='k-grid-content k-virtual-content']/div/table/colgroup/tbody/tr[1]/td[1]/a").text()
        print( 'final Ponumber is: '+ponumber)

    def Pr_out(self):
        self.driver.quit()


```python
在这里插入代码片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值