用硒刮痧。没有这样的元素:由于循环无法定位元素?

时间:2017-09-21 07:11:21

标签: python selenium web-scraping

我正在尝试访问页面中的每个名称,然后抓取HTML代码。由于有多个页面,我试图使用循环。我想访问的每个“行”都有相同的xpath;唯一改变的是它中的数字。 当我尝试运行代码时,我得到No such element: Unable to locate element。是因为str(x)不适合xpath吗?

import time
import os 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
from bs4 import BeautifulSoup as soup 
from selenium.common.exceptions import NoSuchElementException
import glob 
import re 
import shutil

def check_exists_by_xpath(xpath):
    try: 
       driver.find_element_by_xpath(xpath)
    except NoSuchElementException:
       return False
    return True  

base_url =r"https://threedecks.org/index.php?display_type=crewman_search"

path_driver = r"C:\Program Files 
(x86)\Google\Chrome\Application\chromedriver.exe"

driver = webdriver.Chrome(path_driver)    
driver.get(base_url)

pic_path0="//*[@id=\"nation\"]"
driver.find_element_by_xpath(pic_path0).send_keys('Great Britain')
time.sleep(1)
pic_path1="//*[@id=\"roles\"]"
driver.find_element_by_xpath(pic_path1).send_keys('Sailor')
pic_path2="//*[@id=\"form_row_Search\"]/input"  
driver.find_element_by_xpath(pic_path2).click()

for x in xrange(0,51):
    pic_path3="//*[@id=\"crew_search_results\"]/tbody/tr[" + str(x)
    +"]/td[2]/a"

    driver.find_element_by_xpath(pic_path3).click()
    info=get_attribute("href")
    time.sleep(1)

    scraped = open('C:\Users\emarchesi\Desktop\links.txt',"wt")
    scraped.write(info)
    scraped.close()

0 个答案:

没有答案