Xpath路徑中帶入變量


在很多xpath定位中會需要帶入變量,以達到傳入參數的目的
 1     #給指定人發消息
 2     def SendMessege(self,who,what):
 3         # 用xpath定位,xpath(u"//classname[contains(@text,text)]")
 4         self.search(who)
 5         time.sleep(3)
 6         #點擊選擇聯系人
 7 
 8 #    self.driver.find_element_by_id('com.tencent.mm:id/kz').click()
 9     #路徑中帶入變量
10         path = "//android.widget.TextView[contains(@text,'"+who+"')]"
11         self.driver.find_element_by_xpath(path).click()
12 #        UiSelector = "new UiSelector().id(com.tencent.mm:id/kz).classname(android.widget.TextView).text('"+who+"')"
13 #        self.driver.find_element_by_android_uiautomator(UiSelector).click()
14         time.sleep(1)
15         # 清空輸入框
16         self.driver.find_element_by_class_name('android.widget.EditText').clear()
17         # 輸入文字內容
18         self.driver.find_element_by_class_name('android.widget.EditText').send_keys(what)
19         time.sleep(1)
20         # 點擊表情
21         self.driver.find_element_by_accessibility_id(u'表情').click()
22         time.sleep(1)
23         # 選擇表情
24         self.driver.find_element_by_xpath(
25             "//android.widget.GridView/android.widget.FrameLayout[13]/com.tencent.mm.ui.MMImageView").click()
26         time.sleep(2)
27         # 發送消息
28         self.driver.find_element_by_id("com.tencent.mm:id/a1u").click()

 

 
變量用str,整形轉化為字符串
 1 TDs = driver.find_elements_by_tag_name('td')
 2         try:
 3             for INPUT in inputs:
 4                 for POnumber in range(1,101):
 5                     #訂單發送狀態,所有訂單抬頭中發送狀態,變量用str,整形轉化為字符串
 6                     path = "//*[@id='table-head']/tbody/tr["+str(POnumber)+"]/td[10]/span"
 7                     #獲取所有發送狀態的文本屬性
 8                     noSendtext = driver.find_element_by_xpath(path).text
 9                     #勾選框class屬性為mmg-check,發送狀態為未發送時,選中
10                     if INPUT.get_attribute('class') == 'mmg-check' and noSendtext == u'未發送':
11                         INPUT.click()
12                     else:
13                         continue
14                     POnumber += 1
15         except NoSuchElementException as e:
16             return False

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM