下面是我的代码,
[Paused after "<step text>" - press enter to continue]
上面代码的输出是下面的
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
xpath='//ol[@class="ranking"]'
driver=webdriver.Ie("C:\Users\test\Downloads\IEDriverServer_Win32_3.0.0\IEDriverServer.exe")
driver.get('https://www.havocscope.com/country-profile/')
state=[states.find_elements_by_css_selector('li a') for states in driver.find_elements_by_xpath(xpath)]
print (state)
现在,我对如何通过单击每个链接,转到下一页并返回导航来进行迭代感到困惑。 我在下面尝试了for循环,但是没有用。另外,如果我在代码中包括print len(state),它会显示1.可能是因为“ state”是列表中的列表(请注意print(状态)的o / p中有2个方括号)。如果是这样,我如何将其转换为一个列表?
[[<selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="3cc4a03b-9786-4fc6-9698-012f0665bba3")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="5d323e16-451a-4e10-a514-312770ff84bc")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="da69e2ea-4a48-468f-951f-85d6df7d6888")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="9a37d096-dea9-4f76-9680-e3166abebcae")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8d666290-67ed-4290-9fde-9d4e166a5400")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="7b3ba305-1f4e-4723-b898-ef8df1b21e1c")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8604ff20-3bcf-4df3-a66c-57a22c263b13")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="4f20ea21-942a-43cb-8b72-a20fbd0792a9")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="4d54c7aa-fe98-4b5c-8637-01fe48f196f2")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="88a684ed-a49b-44b9-8f34-100c8356a335")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="a2eb4247-8898-4d44-9a83-d260ee4d3385")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="af760bd6-f413-4086-ad83-bcbe03cb4956")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="d47dcb8d-8228-4908-831e-c64aad15f0c2")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="9f2a7747-99ad-4c6c-83bc-6dbd6dad1690")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="86b40883-f289-4a51-af8e-36eddfdac163")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="726f6fec-f93e-4da7-8f18-d23c1c2bc710")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="fb460c1b-a7fb-4dcd-9bd1-ead6bea63a22")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="dfa5b06a-18a6-4317-ba37-dded6e333b83")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="50fd97ad-802c-4956-a535-971b29cc564d")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ac2f8180-0c6b-4f75-b141-ec899556bd47")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="940e5f2b-2367-45b7-ab57-36c684e6ab7c")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="0eb98392-9955-401e-8b8c-afca5d19b630")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8cf3e4a8-c497-4e44-b1ec-0208f2a28cd8")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="7f9dbd8d-a4cc-40a5-adbb-459ec52d60df")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="da237e2e-ab69-4f49-b16e-ae32e2b9f413")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="0a6b7174-904c-4429-8774-5234860d2537")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="29dae3bb-bc9f-4df4-8af3-5cb743535777")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="405f14b6-8893-4ed6-8190-656d0e0b4a1d")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="79e30290-2d14-4cde-95df-735d34a79d92")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="91684d86-0f40-485c-a898-ab7d9e30ee79")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="42d22a9e-5243-44ef-b22f-572a69855de6")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="f7e3319c-3f47-4071-9c14-a55a8a449018")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8b61ca05-0d11-4441-bd06-fa890b05c5e1")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="e0f9fb4c-9eec-4f5b-9e42-ef1088d01530")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ac9b04d3-89d1-416a-bcb6-1a93887660e9")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="4140a5b4-36b3-452b-b982-c32b4a11a93e")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="0baa6ce4-cbf9-4e0d-9e46-f4822c0887e9")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="d0c9ef01-5000-402e-9d21-5229d73b5d6e")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="42702842-e3ab-4003-ad27-375aa41cd217")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="013c29bd-cfa2-47e6-9f00-5e5f94d80a4f")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="50e0b8f6-d08d-411c-be4e-6e5520537e29")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="47f44233-90f5-40d7-841b-af854a1d15c5")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="d401447c-ff98-46c8-bf90-6cc4a2025c43")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="c2968ca7-0a7c-48f9-9a15-d0205d82d3ff")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="344f4af6-3a00-4d5d-8beb-94b9738ff460")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8cdfe780-d6a6-47f3-86ca-4781e24bbfae")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="c028291b-defc-4468-862b-95f1dbb5424c")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="6a7d969b-8470-46cb-8492-d5e1636b607d")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="a9d40a2b-a0cc-44a8-844d-bb748fd0ed4f")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="505685b3-2aeb-4868-866e-5eaf80646a71")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="7427c0c4-d429-4fcc-830e-2f6fec1be49c")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="6d5ac707-9ff4-4d2c-817a-c0f6621cc183")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="47b8ef86-3684-46e9-89a1-fe1459020569")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="436b0a8e-fb5b-426f-9681-7d97c66550f0")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="aaf68e53-78e1-4729-8ccc-e0c5f377f3db")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="11b1eec1-6452-4659-8b55-1cd89f613c95")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="5082275b-acd7-47e8-aef0-d6e0c621a636")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="c4a40605-fa6b-4b29-a6e8-cc54dc300d94")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="5310c7dd-ca10-4225-b2d3-63398b36d54f")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="acbb0b10-5de8-49a0-a486-dbbdd3d5653c")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ed203cf1-1614-4374-b753-1895252dbc8e")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="3899983b-676c-4ba6-9871-8b279019ce67")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="92040605-22c1-4650-a0c6-e6c1710e0205")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="d0e36a46-a49e-4e4a-98dc-3e4584fb0a6a")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="5cdc85a0-c698-4af4-90c3-0866ec7b757a")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="6742d9e2-cf95-4c99-b90c-ddeb04ffe4e6")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="4b769a03-87b5-4c82-90e6-65d6a698953e")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ed41eeef-5ba6-4964-83ad-4e09657aabb9")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="97c07b5a-77df-4342-9293-4f30cda0274a")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ed9898fc-2baa-4995-8d10-6e72af6fd7ac")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ab8fe5a8-2867-48d1-ad8b-a51b103129e3")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="f1b10c0b-fe49-4bc1-9025-9e3e7c5bd603")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8a65e6f5-1cb0-4457-849b-60eaabc31968")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="19a08c63-cbcc-4fed-b628-18375d332dd1")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="b7942bf7-141a-4875-85d2-253c99534b4b")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ffb5f44d-b2ea-41a9-8c2f-b43c793e821a")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8889aeb1-21bd-45d0-adea-bbcef8544e9a")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="673f5ff1-8d3d-4feb-98f0-0390abdbb715")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="86306a74-c512-4d2e-ac60-11996e3a0e42")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="1b2df781-70e0-4123-81c8-0b86ec8f2c12")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8099ce05-0b81-47d3-9f8e-48f18cc300c0")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="b192a83a-ec5d-40cf-9027-95cb11a15f4c")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="829221a9-2504-4ee2-876e-758b0b7e87e2")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="bd24c85d-f3b8-461e-9bd4-228231ffe3bd")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="8b6b5ee3-fa9b-4075-95a6-acc622f6cfc5")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="7b869364-ca2d-40f4-aa8d-5d116c04e005")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="81f8ee44-2009-4a46-93d4-02a23cd92986")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="99d26d1a-7793-4a6b-89fb-c486aeac16ec")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="b5d98ac3-d171-4071-b4aa-6731b1c5c7f7")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="ba3872e9-cd7d-4119-9a6f-07d61b38b8a7")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="f724357e-5182-4c13-9cf6-4f95e8f1a747")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="5356695e-614c-44d8-859d-98f4d2ee4639")>, <selenium.webdriver.remote.webelement.WebElement (session="8745dbe7-bd85-4a9f-a5b4-140dec901b65", element="1f74ff86-f212-4331-bbb5-7adc4692541a")>]]
下面是html
for i in range (0,len(state)):
state[i].click()
答案 0 :(得分:1)
state
已经是列表,因为您使用的是states.find_elements_by_css_selector
而不是states.find_element_by_css_selector
因此将其更改为更像这样的内容:
ranking = driver.find_element_by_xpath(xpath) # notice I use element
num_countries=len(ranking.find_elements_by_css_selector('li'))
for i in range(num_countries):
# you need to refetch countries every time so the elements aren't stale
ranking = driver.find_element_by_xpath(xpath)
countries = ranking.find_elements_by_css_selector('li a')
country = countries[i]
# do what you need with the state now
country()
assert country == driver.find_element_by_css_selector('#content_box li h2').text # make sure the country is the correct one
# go back
driver.back()
注意:因为它们是国家,所以我将var重命名为countries
。