刮除隐藏元素

时间:2018-08-22 15:40:26

标签: python html selenium

我的问题是双重的:

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)中抓取特定的代码历史记录数据。用户必须登录,因此是我上面的第一个问题,在浏览器中查看所需信息的方法是展开浅紫色的“代码历史记录”表。我需要的具体信息是“操作”列为“已添加”且“注释”列为“已添加代码”的任何行中的日期:

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

2 个答案:

答案 0 :(得分:3)

页面上有两种形式,分别带有输入@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()

答案 1 :(得分:1)

要登录该网站,您需要诱使 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
    
相关问题