简体   繁体   中英

Compare elements using locators in web table Selenium + Python

So idea is - I need to check that "green arrow" number same with "red arrow" number. THIS NUMBERS always generate with different numbers. So i make a variables "first_value" for the "red arrow" and "second_value" for the "green arrow". And everything is working fine(i think), BUT I use ugly Xpath, so can someone help me make my Xpath's a little bit more beautiful OR provide another locator, please? I dont have idea which locator i can use in my case

first_value = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/form/div[4]/div[2]/div/table/tbody/tr[4]/td/div[1]/div[2]/div[2]/table/tbody/tr[5]/td/table/tbody/tr[2]/td/div/div/table/tbody/tr[2]/td[7]"))).text

second_value = wait.until(EC.text_to_be_present_in_element((By.XPATH, "/html/body/form/div[4]/div[2]/div/table/tbody/tr[4]/td/div[1]/div[2]/div[2]/table/tbody/tr[5]/td/table/tbody/tr[2]/td/div/div/table/tbody/tr[1]/td[1]"), (first_value)))

enter image description here

You can rather extract the whole HTML of the table using get_attribute('outerHTML') . Then parse the whole table using either the pandas library or some simple parser like this: https://github.com/yuanxu-li/html-table-extractor which will give you a python object.

And then compare the value. This should be less error-prone.

OR

start your XPATHs with //tr[@class='GridRowDefault']/... and //tr[@class='GridAltRowDefault']/... respectively.

Update: This is a good extension to help you determine xpaths: https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en

The following XPath:

//tr[@class="GridAltRow_Default"][1]/td[1]=//tr[@class="GridAltRow_Default"][2]/td[7]

Will output TRUE if the green arrow number is equal to the red arrow number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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