【问题标题】:Scraping hidden elements抓取隐藏元素
【发布时间】:2018-08-22 15:40:25
【问题描述】:

我的问题有两个:

1) 我正在尝试使用下面的代码登录到这个page,来源是here。可以使用我提供的凭据,该凭据将在 28 天后过期,但在此之后为查看此内容的人创建一个试用帐户相对容易。

from selenium import webdriver
driver_path = 'Path to my downloaded chromedriver.exe file'
url_login = 'https://www.findacode.com/signin.html'
username = 'jd@mailinator.com'
password = 'm%$)-Y95*^.1Gin+'

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(executable_path=driver_path, chrome_options=options)

driver.get(url_login)
assert '_submit_check' in driver.page_source
driver.find_element_by_name('id').send_keys(username)
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_xpath("//input[@value='Sign In']").submit()

我收到以下所有 3 个元素的错误:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

我对 html/css/javscript 的命令没有那么强,但我已经尝试按照 thread 使用等待并收到超时。打算接下来尝试该线程中的 ActionChains,但很想听听对此有更多了解的人如何继续。

2) 最终,我想通过在循环中改变代码(url 的最后 5 个字符)从这个 url(源 here)中刮取特定的代码历史数据。用户必须登录,因此我上面的第一个问题,在浏览器中查看我所追求的信息的方法是展开浅紫色的“代码历史”表。我要查找的具体信息是 Action 列为“添加”且注释列为“代码添加”的任何行的日期:

Date       Action Notes 
2018-01-01 Added  First appearance in code
2017-02-01 Added  Code Added

我的问题是,由于我认为隐藏的表格需要通过单击来展开以公开我所追求的数据,我该如何进行?

编辑 这是解释我的第二个问题的代码、伪代码和注释。

url_code = "https://www.findacode.com/code.php?set=CPT&c="
driver.get(url_code+'0001U') # i'm presuming that this will preserve the login session
driver.find_element_by_id('history').click() # i intend for this to expand the Code History section and expose the table shown earlier in the post but it's not doing that
check whether the phrase "Code Added" occurs in page source
if so, grab the date that is in the <td nowrap> tag that is 2 tags to the left

如果不能使用 Selenium,我可以在最后两行使用 BeautifulSoup,但我需要帮助理解为什么我没有看到我想要抓取的数据

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    页面上有两个表单,输入 @name="id"@name="password" 和“登录”按钮。第一个是隐藏的。您需要使用@name="login" 处理表单:

    form = driver.find_element_by_name('login')
    form.find_element_by_name('id').send_keys(username)
    form.find_element_by_name('password').send_keys(password)
    form.find_element_by_xpath("//input[@value='Sign In']").submit()
    

    【讨论】:

    • 感谢您指出这一点。仍然加快了 html 的速度。对我的问题的第二部分有什么想法吗?
    • 第二部分没有代码,所以问题不太清楚...如果您需要获取多个更改页码的URL,您可以定义counter = 1并在a中使用driver.get('https://www.findacode.com/code.php?set=CPT&amp;c=000%sU' % counter)循环 + 每次迭代将 counter 递增 1
    • 我会尝试为此添加一些代码,但我的挑战是更多关于如何获取表格内容
    • 那么还有第三个问题吗? :) 最好为每个问题创建票证,因为很难分析代码并搜索根本原因
    • 没有第 3 期,只有第 2 期,显然可能没有清楚地表达。我只是澄清一下,第二个问题更多是关于获取数据而不是循环代码。
    【解决方案2】:

    要登录本网站,您需要诱导 WebDriverWait 以使所需的元素可点击,您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      driver_path = 'Path to my downloaded chromedriver.exe file'
      url_login = 'https://www.findacode.com/signin.html'
      username = 'jd@mailinator.com'
      password = 'm%$)-Y95*^.1Gin+'
      
      options = webdriver.ChromeOptions()
      options.add_argument('--headless')
      options.add_argument("start-maximized")
      options.add_argument('disable-infobars')
      driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get(url_login)
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[@name='login']//input[@name='id']"))).send_keys(username)
      driver.find_element_by_xpath("//form[@name='login']//input[@name='password']").send_keys(password)
      driver.find_element_by_xpath("//form[@name='login']//input[contains(@value,'Sign In')]").submit()
      print("Logged In successfully")
      
    • 控制台输出:

      Logged In successfully
      

    【讨论】:

    • 你能解释一下为什么 OP 需要 诱导 WebDriverWait 而它在没有 WebDriverWait 的情况下也能完美运行吗?
    • @Andersson 他在他的每一篇文章中都要求使用 webdriver 等等,我仍然不知道他为什么这样做!
    猜你喜欢
    • 2019-06-18
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    相关资源
    最近更新 更多