Unable to fit the two expressions into my script

爱⌒轻易说出口 提交于 2020-01-17 08:04:33

问题


I've written a script to scrape documents from a web page using python in combination with selenium. However, the only thing I got stuck is print the value. As selenium doesn't support indexing in text, I can't think further to accomplish this. Taking a look into my code You will get to know what I meant. I've commented out the two lines to be rectified. Thanks in advance. Here is what I've written so far:

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('https://www.aopa.org/learntofly/school/')

driver.find_element_by_id('searchTerm').send_keys('All')
time.sleep(5)
driver.find_element_by_id('btnSearch').click()
time.sleep(3)
for items in driver.find_elements_by_xpath('//td/a'):
    driver.execute_script(items.get_attribute('href'))
    time.sleep(3)
    for docs in driver.find_elements_by_xpath('//div[@id="schoolDetail"]'):                              
        print(docs.text)

        # Instead of docs I wanted to print the two expressions but can't

        # Name = docs.find_elements_by_xpath('//div[@id="schoolDetail"]/text()[1]') 
        # Address = docs.find_elements_by_xpath('//div[@id="schoolDetail"]/text()[2]')
        # print(Name, Address)

    for back_links in driver.find_elements_by_xpath('//div[@id="schoolDetail"]//h4/a'):
        driver.execute_script(back_links.get_attribute('href'))

driver.quit()

回答1:


Try below code and let me know in case of any issues:

Name = driver.execute_script('return arguments[0].childNodes[1].textContent', docs) # To get Name text value
Address = driver.execute_script('return arguments[0].childNodes[3].textContent', docs) # To get Address text value



回答2:


your commented expressions are retrieving webelements. I'm assuming that there is just one name, so the code should use find_element_by_xpath (without the "s"). If you want the text that appears in those elements, ask for that attribute:

    Name = docs.find_element_by_xpath('//div[@id="schoolDetail"]/text()[1]').text
    Address = docs.find_element_by_xpath('//div[@id="schoolDetail"]/text()[2]').text


来源:https://stackoverflow.com/questions/44843978/unable-to-fit-the-two-expressions-into-my-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!