Home > Enterprise >  python selenium xpath loop skips random elements
python selenium xpath loop skips random elements

Time:05-07

I was trying to select all the 264 Recipients from the second form on this website and I used the code below:

s = Service('./chromedriver.exe')
driver = webdriver.Chrome(service=s)
url = "https://armstrade.sipri.org/armstrade/page/trade_register.php"
driver.get(url)
sleep(5)

and here's the loop:

for b in range(1, 264):
    Recipients = driver.find_element(By.XPATH, "//*[@id='selFrombuyer_country_code']/option[%d]" % b)
    Recipients.click()
    sleep(0.2)
    # click "ADD" option
    driver.find_element(By.XPATH, "/html/body/font/div/form/table/tbody/tr[3]/td/table/tbody/tr/td[2]/input[1]").click()
    sleep(0.2)

When I test this code, the loop worked...partly: it skips random elements and only got like half of the elements selected.

And here are some of the elements my loop ignored, which look perfectly normal:

//*[@id="selFrombuyer_country_code"]/option[2]

//*[@id="selFrombuyer_country_code"]/option[26]

Why can't my loop traverse all the elements?

CodePudding user response:

This should work for you. No hard-coded ranges:

# Below are the Additional imports needed
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Code after the driver is instantiated
driver.get("https://armstrade.sipri.org/armstrade/page/trade_register.php")
c_codes = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='selFrombuyer_country_code']//option")))
print(len(c_codes))
ls1=[]
ls2=[]
for each_code in c_codes:
    ls1.append(each_code.text)
    each_code.click()
    time.sleep(0.2)
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='selFrombuyer_country_code']/..//..//input[contains(@value, 'Add')]"))).click()
all_c = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@name='selselTobuyer_country_code']//option")))
for each_c in all_c:
    ls2.append(each_c.text)
print(len(ls1))
print(len(ls2))
print(ls1)
print(ls2)
driver.close()
  • Related