【问题标题】:How to print the open pdf link from using Selenium in Python?如何在 Python 中使用 Selenium 打印打开的 pdf 链接?
【发布时间】:2019-10-18 16:41:28
【问题描述】:

我无法打印运行给定代码后打开的最终 pdf 的链接

from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException 

def page_is_loaded(driver):
    return driver.find_element_by_tag_name("body")!= None


def check_exists_by_text(text):
    try:
        driver.find_element_by_link_text(text)
    except NoSuchElementException:
        return False
    return True

driver = webdriver.Chrome("C:/Users/Roshan/Desktop/sbi/chromedriver")
driver.maximize_window()
driver.get("http://www.careratings.com/brief-rationale.aspx")

wait = ui.WebDriverWait(driver,10)
wait.until(page_is_loaded)

location_field = driver.find_element_by_name("txtfromdate")
location_field.send_keys("2019-05-06")

last_date = driver.find_element_by_name("txttodate")
last_date.send_keys("2019-05-21")

driver.find_element_by_xpath("//input[@name='btn_submit']").click()

if check_exists_by_text('Reliance Capital Limited'):
    elm =driver.find_element_by_link_text('Reliance Capital Limited')
    driver.implicitly_wait(5)
    elm.click()
    driver.implicitly_wait(50)
    #time.sleep(5)
    #driver.quit()
else :
    print("Company is not rated in the given Date range")

我希望实际输出是这个 pdf 的链接:

http://www.careratings.com/upload/CompanyFiles/PR/Reliance%20Capital%20Ltd.-05-18-2019.pdf

但我不知道如何打印此链接

【问题讨论】:

标签: python selenium web-scraping beautifulsoup selenium-chromedriver


【解决方案1】:

您需要找到表格中的所有元素,然后从中提取数据。

from selenium import webdriver
import os

# setup path to chrome driver
chrome_driver = os.getcwd() + '/chromedriver'
# initialise chrome driver
browser = webdriver.Chrome(chrome_driver)
# load url
browser.get('http://www.careratings.com/brief-rationale.aspx')

# setup date range
location_field = browser.find_element_by_name("txtfromdate")
location_field.send_keys("2019-05-06")
last_date = browser.find_element_by_name("txttodate")
last_date.send_keys("2019-05-21")
browser.find_element_by_xpath("//input[@name='btn_submit']").click()

# get all data rows
content = browser.find_elements_by_xpath('//*[@id="divManagementSpeak"]/table/tbody/tr/td/a')

# get text and href link from each element
collected_data = []
for item in content:
    url = item.get_attribute("href")
    description = item.get_attribute("innerText")
    collected_data.append((url, description ))

输出:

('http://www.careratings.com/upload/CompanyFiles/PR/Ashwini%20Frozen%20Foods-05-21-2019.pdf', 'Ashwini Frozen Foods')
('http://www.careratings.com/upload/CompanyFiles/PR/Vanita%20Cold%20Storage-05-21-2019.pdf', 'Vanita Cold Storage') 

等等

【讨论】:

    【解决方案2】:

    我会说你只需要写这行:

    pdf_link = elm.get_attribute("href")
    

    【讨论】:

      【解决方案3】:

      看看下面的图片。您错过了一个要点击的重要部分。当您在该输入框中输入一些文本时,会出现一个向下投影的下拉菜单,显示他们股票中可供选择的搜索结果。一旦你点击它,其余的都是原样。

      试试下面的脚本:

      import time
      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.common.keys import Keys
      from selenium.webdriver.support.wait import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      url = "http://www.careratings.com/brief-rationale.aspx"
      
      with webdriver.Chrome() as driver:
          driver.get(url)
          wait = WebDriverWait(driver,10)
      
          location_field = wait.until(EC.presence_of_element_located((By.NAME, "txtfromdate")))
          location_field.send_keys("2019-05-06")
      
          last_date = wait.until(EC.presence_of_element_located((By.NAME, "txttodate")))
          last_date.send_keys("2019-05-21")
      
          input_search = wait.until(EC.presence_of_element_located((By.NAME, "txtSearchCompany_brief")))
          input_search.send_keys('Reliance Capital Limited')
      
          time.sleep(3) #could not get rid of this hardcoded delay to make the script work
      
          wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"[onclick*='Reliance Capital Limited']"))).click()
      
          # time.sleep(2) #activate this line in case the script behaves otherwise
      
          wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[name='btn_submit']"))).click()
          for item in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"table tr td > a[href$='.pdf']"))):
              print(item.get_attribute("href"))
      

      【讨论】:

        猜你喜欢
        • 2022-11-23
        • 1970-01-01
        • 2019-06-02
        • 2022-08-16
        • 1970-01-01
        • 1970-01-01
        • 2022-07-08
        • 2022-06-12
        • 2021-06-26
        相关资源
        最近更新 更多