我认为这个问题确实很简单,但是我不知道该怎么办。 我有一个XPath元素,其编号序列为1到8。我需要一个一个地打开所有这些元素。
//*[@id="meio"]/div[2]/section/article[1]/h2/span/a[1]
//*[@id="meio"]/div[2]/section/article[2]/h2/span/a[1]
//*[@id="meio"]/div[2]/section/article[3]/h2/span/a[1]
//*[@id="meio"]/div[2]/section/article[4]/h2/span/a[1]
//*[@id="meio"]/div[2]/section/article[5]/h2/span/a[1]
//*[@id="meio"]/div[2]/section/article[6]/h2/span/a[1]
//*[@id="meio"]/div[2]/section/article[7]/h2/span/a[1]
//*[@id="meio"]/div[2]/section/article[8]/h2/span/a[1]
我正在尝试这样做:
while w > 0:
i = 0
a = str(i)
while i < 8:
i += 1
driver.find_element_by_xpath('//*[@id="meio"]/div[2]/section/article[" + str(i) + "]/h2/span/a').click()
driver.refresh()
#there's more code after this, but I don't think it's necessary.
但是它执行了我在第一页上需要做的事情,然后返回并永远在第一页中循环播放。我无法单击XPath 2或除1之外的任何其他按钮。我在做什么错了?
每次循环开始时,我需要在XPath中增加一个数字,该怎么做?
答案 0 :(得分:0)
这样的事情怎么办?
articles_with_links = driver.find_elements_by_xpath('//*[@id="meio"]/div[2]/section/article')
for article in articles_with_links:
article.find_element_by_xpath('//h2/span/a').click()