我在上网,但是我总是遇到麻烦...
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素
中有错误
click = '//*[@id="pagerTagAnchor' + str(j) + '"]'
driver.find_element_by_xpath(click).click()
我该怎么办?
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
f = open('movie.txt', 'w', encoding='utf-8')
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")
driver = webdriver.Chrome('C:/Users/Administrator/Downloads/chromedriver', chrome_options=options)
for i in range(1, 101):
for j in range(1, 11):
driver.get('https://movie.naver.com/movie/bi/mi/point.nhn?code=167638')
driver.implicitly_wait(20)
click = '//*[@id="pagerTagAnchor' + str(j) + '"]'
driver.find_element_by_xpath(click).click()
response = requests.get(str(driver.current_url))
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
notices1 = soup.select(
'body > div > div > div.score_result > ul > li:nth-of-type(' + str(j) + ') > div.score_reple > p')
for n in notices1:
a = n.text.strip()
a = str(a).replace(",", "")
f.write(str(a) + ',')
f.close()
这是完整的和弦。
答案 0 :(得分:0)
我们必须等待定位元素。这是通过xpath查找元素的功能
import logging
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
def findByXpath(locator, timeout = None):
''' Get one item by xpath'''
if not timeout:
timeout = 10
try:
element = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.XPATH, locator))
)
return element
except TimeoutException:
logging.info(' Find by xpath not found : %s', locator)
logging.debug('%s', TimeoutException)
return None