繁体   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.

但是在我们的网站测试中,我们需要做一些更高级的操作。 web 表类似于演示网站http://demo.guru99.com/test/web-table-element.php

问题:

  1. 我的表行值将是动态的,例如第一次加载(“BEML Ltd. A 253.1 327.4 + 3.7”)行号将是第 6 位。 也许下次该行将显示在第 10 位,因为添加了更多行。
  2. 现在如何从第三列(“Previous close RS”)中找到特定值,例如“253.1”?
  3. 根据匹配值,我必须单击第一列中可用的相应按钮(例如“BEML Ltd”-web 站点链接)。 演示网站在第一列中只有一个链接。 但是我的测试网站有三个按钮:Hold、Close、Open。

我尝试使用以下代码,但无法单击并导航到相应的屏幕。

   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 我问如何匹配动态增长 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