带有driver.find_element_by_xpath的Selenium NoSuchElementException

时间:2016-01-23 09:13:05

标签: python python-3.x selenium

第一个问题:如何让python最小化chrome?

第二个问题:当使用下一个按钮进入结束页面时,我如何告诉python继续...而不是给我一个错误?

import pygame, sys
from pygame.locals import *

pygame.init()

class player:
  def __init__(self):
    self.x = 100
    self.y = 100
    self.image = pygame.image.load('Data/chracter.png')
    self.update(0)

  def update(self,pos):
    if pos != 0:
        self.x += pos
    display.blit(self.image,(self.x,self.y))

clock = pygame.time.Clock()

background_img = pygame.image.load('Data/background.jpg')
size = background_img.get_size()

pygame.mixer.init()                         
pygame.mixer.music.load('Data/song.wav')   
pygame.mixer.music.set_volume(0.7)          
pygame.mixer.music.play(-1)    

display = pygame.display.set_mode(size)

pygame.display.set_caption('Game')  

player1 = player()

while 1: 
  player1.update(0)

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()

    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            player1.update(-10)
        elif event.key == pygame.K_RIGHT:
            player1.update(10)

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            player1.update(0)
        elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            player1.update(0)           

  display.blit(background_img,(0,0))

  pygame.display.update()
  clock.tick(60)

执行此代码后,我收到以下错误:

driver.get("https://www.google.com/#q=" + user_input)
for n in range(5):
    driver.find_element_by_xpath("""//*[@id="pnnext"]/span[2]""").click()
    time.sleep(2)
    posts2 = driver.find_elements_by_class_name("_Rm")
    for post2 in posts2:
        print(post2.text)
        text_file.write(post2.text + "\n")

1 个答案:

答案 0 :(得分:1)

你得到NoSuchElementException,驱动程序无法找到你的元素。将您的xpath更改为此

driver.find_element_by_xpath("//*[@id='pnnext']/span[2]").click()

没有3个引号。