简体   繁体   English

如何使用 Selenium 和 Python 在动态 web 表中单击可用按钮

[英]How to click button available in dynamic web table using Selenium and Python

I am new to Selenium, I have gone through the website tutorialhttps://www.guru99.com/handling-dynamic-selenium-webdriver.html to learn how to get the row count & column count for the web table. I am new to Selenium, I have gone through the website tutorialhttps://www.guru99.com/handling-dynamic-selenium-webdriver.html to learn how to get the row count & column count for the web table.

But in our website testing we need to do a few more advanced operations.但是在我们的网站测试中,我们需要做一些更高级的操作。 The web table is similar to demo website http://demo.guru99.com/test/web-table-element.php . web 表类似于演示网站http://demo.guru99.com/test/web-table-element.php

Questions:问题:

  1. My table row value will be dynamic, for example first time loading ("BEML Ltd. A 253.1 327.4 + 3.7") row number will be 6th place.我的表行值将是动态的,例如第一次加载(“BEML Ltd. A 253.1 327.4 + 3.7”)行号将是第 6 位。 Maybe next time the row will display on 10th place, due to few more rows being added.也许下次该行将显示在第 10 位,因为添加了更多行。
  2. Now how to find the particular value from the third column ("Previous close RS") for example "253.1"?现在如何从第三列(“Previous close RS”)中找到特定值,例如“253.1”?
  3. Based on the match value I have to click the corresponding button available in the first column (Example "BEML Ltd" - web site link).根据匹配值,我必须单击第一列中可用的相应按钮(例如“BEML Ltd”-web 站点链接)。 The demo website has only one link in fist column.演示网站在第一列中只有一个链接。 But my test website has three buttons: Hold, Close, Open.但是我的测试网站有三个按钮:Hold、Close、Open。

I have tried using the below code, but I am not able click and navigate the to corresponding screen.我尝试使用以下代码,但无法单击并导航到相应的屏幕。

   try:
            WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'td')))
            table= self.driver.find_element(By.ID, 'TableGrid')
            trs = table.find_elements_by_tag_name('tr')
            for tr in trs:
                tds = tr.find_elements_by_tag_name('td')
                for td in tds:
                    match_obj = re.search('searching Text', td.text)
                    print(match_obj)
                    if match_obj and match_obj.group(1) == '0':
                        success_button = tr.find_element_by_css_selector('button.btn-success')
                        print(success_button.get_attribute('type'))
                        success_button.click()
        except :
              pass

OK.simply I am asking how to match particular value in any columns in the dynamic growing web table ( the row index is not fixed) and click the button available in the particular row of the first column? OK.simply 我问如何匹配动态增长 web 表(行索引不固定)中任何列中的特定值,然后单击第一列特定行中可用的按钮?

try:
    WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'td')))
    table= self.driver.find_element(By.ID, 'TableGrid')
    trs = table.find_elements_by_tag_name('tr')
    row_count = 0
    matched_row_count = 0
    for tr in trs:
        tds = tr.find_elements_by_tag_name('td')
        time.sleep(2)
        for i, td in enumerate(tds):
            # This for loop will iterate single row value. Here i is index value 
            if i == 1 and td.text == 'required matching column value':
               #print("Matched {}: {}".format(i, td.text))
               matched_row_count = row_count
               print("matched_row_count :",matched_row_count)
        row_count = row_count+1
        # will print all the column name  #//*[@id="TableGrid"]/div[1]/table/tbody/tr[4]/td[1]/a[3]
            # tr[4] -row [td1]-column a[3]-button
except :
      print("could not found the column details")

element = self.driver.find_element(By.CSS_SELECTOR, "tr:nth-child("+matched_row_count+")"+" "+".btn-info")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
element = self.driver.find_element(By.CSS_SELECTOR, "body")
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
self.driver.find_element(By.CSS_SELECTOR,  "tr:nth-child("+matched_row_count+")"+" "+".btn-info").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM