如何在xpath selenium(python)中创建for循环
因此,我一直在编写这段代码,从维基百科搜索中获取搜索结果的标题。代码如下:如何在xpath selenium(python)中创建for循环,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,因此,我一直在编写这段代码,从维基百科搜索中获取搜索结果的标题。代码如下: from selenium import webdriver path = "C:\webdrivers\chromedriver.exe" driver = webdriver.Chrome(path) driver.get("https://en.wikipedia.org/w/index.php?cirrusUserTesting=glent_m0&search=1st+indochinese+war&
from selenium import webdriver
path = "C:\webdrivers\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get("https://en.wikipedia.org/w/index.php?cirrusUserTesting=glent_m0&search=1st+indochinese+war&title=Special%3ASearch&go=Go&ns0=1")
x = 1
while x != 10:
headerxpath = '//*[@id="mw-content-text"]/div[3]/ul/li[{}]/div[1]/a'.format(x)
seekheader = driver.find_element_by_xpath(headerxpath)
print(seekheader.text)
x = x + 1
问题是我不确定如何正确使用语法将其放入for循环。因此,它不打印前10个结果,而是打印所有结果。我不能x!=大数字,因为一旦收集到最终标题,就会导致代码失败
希望你们能提供帮助:)像这样的东西应该在
for
循环中打印所有标题(具体是341个结果)。在获取
url后,执行以下操作-这是CSS选择器方式:
#click the last "View" button at the bottom of the page so you can get all results on one page
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "p.mw-search-pager-bottom > a:last-of-type"))).click()
#css matching each of the 341 results
seekheader = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.mw-search-result-heading > a")))
for header in seekheader:
print(header.text)
如果您发现使用xpath更容易,只需定义这些内容,而不是上面的各行:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//p[@class='mw-search-pager-bottom']/a[last()]"))).click()
及
为等待条件添加这些导入:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
类似的内容应该在
for
循环中打印所有标题(具体是341个结果)。在获取
url后,执行以下操作-这是CSS选择器方式:
#click the last "View" button at the bottom of the page so you can get all results on one page
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "p.mw-search-pager-bottom > a:last-of-type"))).click()
#css matching each of the 341 results
seekheader = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.mw-search-result-heading > a")))
for header in seekheader:
print(header.text)
如果您发现使用xpath更容易,只需定义这些内容,而不是上面的各行:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//p[@class='mw-search-pager-bottom']/a[last()]"))).click()
及
为等待条件添加这些导入:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
谢谢,这很有效。只是想知道,我怎样才能真正打印出每个文档的标题而不是标题和描述哦,对不起,这就是你想要的。让我看看。非常感谢:)更新了
seekheader
definition。你现在应该只得到标题。谢谢,真的很感激:)谢谢你的工作。只是想知道,我怎样才能真正打印出每个文档的标题而不是标题和描述哦,对不起,这就是你想要的。让我看看。非常感谢:)更新了seekheader
definition。您现在应该只获得标题。谢谢,非常感谢:)