python selenium xpath 循环跳过随机元素
python selenium xpath loop skips random elements
我试图 select 本网站第二个表格中的所有 264 个收件人,我使用了以下代码:
s = Service('./chromedriver.exe')
driver = webdriver.Chrome(service=s)
url = "https://armstrade.sipri.org/armstrade/page/trade_register.php"
driver.get(url)
sleep(5)
这是循环:
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)
当我测试这段代码时,循环起作用了……部分地:它跳过了随机元素,只得到了一半的元素 selected。
下面是我的循环忽略的一些元素,它们看起来非常正常:
//*[@id="selFrombuyer_country_code"]/option[2]
//*[@id="selFrombuyer_country_code"]/option[26]
为什么我的循环不能遍历所有元素?
这应该适合你。没有 hard-coded 个范围:
# 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()
我试图 select 本网站第二个表格中的所有 264 个收件人,我使用了以下代码:
s = Service('./chromedriver.exe')
driver = webdriver.Chrome(service=s)
url = "https://armstrade.sipri.org/armstrade/page/trade_register.php"
driver.get(url)
sleep(5)
这是循环:
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)
当我测试这段代码时,循环起作用了……部分地:它跳过了随机元素,只得到了一半的元素 selected。
下面是我的循环忽略的一些元素,它们看起来非常正常:
//*[@id="selFrombuyer_country_code"]/option[2]
//*[@id="selFrombuyer_country_code"]/option[26]
为什么我的循环不能遍历所有元素?
这应该适合你。没有 hard-coded 个范围:
# 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()