drutcs7210 2016-01-16 06:44
浏览 311
已采纳

使用相同的URL处理两个不同的页面

I'm working on a program that automatically logs into a site using Selenium. In some situations, if the site doesn't remember the browser, it will ask you to answer a secret question before it takes you to the password page. Originally I thought I would just handle this with an If statement that checks the current URL against the specific URL that I knew was produced when I was directed to the secret question page. The issue I'm running into, is that the URL for the secret question page, and the URL for the password page are the same. Is there a way to differentiate these two pages by the URL in this situation, or will I need to find an alternative method?

  • 写回答

2条回答 默认 最新

  • dongzhi2887 2016-01-16 09:46
    关注

    If the URLs are exactly the same, you won't have any differences to compare, so you won't be able to tell what the page content is based on the URL.

    It's common to have one page, with one URL, that shows different content, determined by backend code that Selenium can't see. You will need to compare something that is different between the two pages, such an element which only appears on one page.

    Presuming that you don't care whether a password or secret question is shown, you just want Selenium to get through the login process, an easy solution is to use an if / else statement to check which form appears and then attempt to fill out the form that is present.

    if ($this->isElementPresent($password_form)) {
        // enter the password;
    } elseif ($this->isElementPresent($secret_question)) {
        // answer the secret question;
    } else {
        throw new Exception('Could not find password form or secret question on the login page. [url]');
    }
    

    The actual form element most likely has a different ID or name for each form that you can use for identification, or the input for password/secret answer will have different names. (If all else fails, you can look for the text on the page which says whether to enter a password or secret question.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题