首页 > 解决方案 > 抓取隐藏元素

问题描述

我的问题是双重的:

1)我正在尝试登录此页面,源代码在这里,使用下面的代码。可以使用我提供的凭据,该凭据将在 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 的命令没有那么强,但我已经尝试在每个线程中使用等待并收到超时。接下来打算从该线程中尝试 ActionChains,但很想听听对此有更多了解的人如何继续。

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

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,但我需要帮助理解为什么我没有看到我想要抓取的数据

标签: pythonhtmlselenium

解决方案


页面上有两个带有输入@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()

推荐阅读