无法使用Selenium WebDriver循环打印数据

时间:2019-01-16 07:40:05

标签: python selenium-webdriver web-scraping

我正在尝试提取地址。文本位于css选择器

class = ng-binding下。当我独立于循环运行地址时,会得到准确的结果。但是,当我将其包含在循环中时,会出现错误:“无法找到元素:p.ng-binding”。该循环可以正常工作,并且可以在多个下拉选项之间进行迭代。

import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

fp = webdriver.FirefoxProfile()


driver = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')

driver.get('http://www.vistaarfinance.com/contact.html')
wait = WebDriverWait(driver, 2)
driver.maximize_window()
#locate all the links which have xml
#s1 = driver.find_element_by_id("state")

obj = Select(driver.find_element_by_name("state"))
options=obj.options
#options = driver.find_elements_by_tag_name('option')
for ele_index in range(len(options)):
    # select a url
    obj = Select(driver.find_element_by_name("state"))
    obj.select_by_index(ele_index)

    obj2 = Select(driver.find_element_by_name("branch"))
    options2=obj2.options
    for ele_index2 in range(len(options2)):
    # select a url
        obj2 = Select(driver.find_element_by_name("branch"))
        obj2.select_by_index(ele_index2)
        time.sleep(14)

        address = driver.find_element_by_css_selector(' p.ng-binding:nth-child(2)')
      #  address = driver.find_element_by_css_selector(' body.ng-scope main.wrap div.hide-menu-container section.contact-form.ng-scope form #contactForm.ng-valid.ng-dirty.ng-valid-parse div.branch-address div.address div.ng-scope p.ng-binding')
        print(address.text)
        #print(address.get_attribute('innerHTML'))

        element2 = obj2.options[ele_index2]
    # ...
    element = obj.options[ele_index]

0 个答案:

没有答案