【问题标题】:How to get text from the tag with Selenium and BeautifulSoup如何使用 Selenium 和 BeautifulSoup 从标签中获取文本
【发布时间】:2019-01-11 10:44:55
【问题描述】:

有我需要得到的验证码的文字描述。但我无法用 BeautifulSoup 得到它。请帮忙解决。

当我运行代码时:“无”是结果。

import os, urllib.request, requests, datetime, time, random, ssl, json, codecs, csv, urllib
from urllib.request import Request, urlopen
from urllib.request import urlretrieve
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
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 NoAlertPresentException
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup


chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chrome_options)
driver.set_window_size(1050, 740)
driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companies")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox']/div[@class='recaptcha-checkbox-checkmark']"))).click()
time.sleep(5)
html = driver.page_source()
soup = BeautifulSoup(html, 'html.parser')
title = soup.find('div',{'class':'rc-imageselect-desc-wrapper'})
print (title)
driver.quit()

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:

    您要查找的内容位于不同的iframe 上,您需要再次切换到框架

    .....
    time.sleep(5)
    # go to parent or top frame
    driver.switch_to.default_content()
    iframe = driver.find_element_by_css_selector('iframe[title="recaptcha challenge"]')
    driver.switch_to.frame(iframe)
    #title = driver.find_element_by_css_selector('.rc-imageselect-desc-wrapper strong') # cars
    title = driver.find_element_by_class_name('rc-imageselect-desc-wrapper') # Select all images with cars.....
    print(title.text)
    
    # with BeautifulSoup
    #html = driver.page_source()
    #soup = BeautifulSoup(html, 'html.parser')
    #title = soup.find('div',{'class':'rc-imageselect-desc-wrapper'})
    #print (title)
    

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2023-01-08
      相关资源
      最近更新 更多