简体   繁体   English

硒:sendKeys()

[英]Selenium: sendKeys()

I tried using Java + Selenium to automatically login to my Gmail account however each time I am blocked by Google. 我尝试使用Java + Selenium来自动登录到我的Gmail帐户,但是每次我被Google阻止时。 When I log in manually (even on the very same server) I have no such issue. 当我手动登录(即使在同一台服务器上)时,也没有这样的问题。 Further, if I use "sendKeys" type lib ( https://pypi.python.org/pypi/SendKeys/0.3 ) I have no such problem. 此外,如果我使用“ sendKeys”类型的lib( https://pypi.python.org/pypi/SendKeys/0.3 ),则不会出现此类问题。

My question is two fold: 我的问题有两个:

  1. What are the tech differences between how Selenium "sends keys" and how a normal user would? Selenium如何“发送密钥”与普通用户之间的技术区别是什么? Or how the Python library does? 还是Python库如何?

  2. Is there a way to modify selenium so it mimics a real user? 有没有办法修改硒,使其模仿真实用户? Or is there another library to use instead of Selenium that mimicks a real user? 还是有另一个库可以代替Selenium来模仿真实用户?

Thanks 谢谢

Actually its the difference of speed. 实际上是速度的差异。 Human being types slowly and script write it very fastly. 人类打字缓慢,脚本书写很快。 So, you have to decrease your script speed. 因此,您必须降低脚本速度。 So on the whole you have to built better bot. 因此,总的来说,您必须构建更好的机器人。 Better bots are not going to be detected by any server. 更好的机器人不会被任何服务器检测到。

您应该在代码中包含一些延迟,以欺骗Google您的代码是真正的人类-就是这样:) Thread.sleep可能是这里最简单的解决方案。

Open gmail & login & open latest received mail 打开gmail&登录并打开最新收到的邮件

DriverObj.get("http://www.gmail.com");
                DriverObj.findElement(By.id("gmail-sign-in")).click();
                DriverObj.findElement(By.id("Email")).sendKeys("youremailID");
                DriverObj.findElement(By.id("next")).click();
                DriverObj.findElement(By.id("Passwd")).sendKeys("yourpassword");
                DriverObj.findElement(By.id("signIn")).click();
                Thread.sleep(2000);
            WebElement tableelement=DriverObj.findElement(By.xpath("//div[@class='Cp']/div/table//tbody"));
            java.util.List<WebElement> tableelement1=tableelement.findElements(By.xpath("//div[@class='Cp']/div/table//tbody/tr"));
            int tableelementsize=tableelement1.size();
            int i;
            if(tableelementsize>0){
                //DriverObj.navigate().refresh();
                for(i=0;i<tableelementsize;i++){
                    DriverObj.findElement(By.xpath("//div[@class='Cp']/div/table//tbody/tr/td[6]")).click();
                }

            }else {
                 if(tableelementsize==0){
                    DriverObj.navigate().refresh();
                    for(i=0;i<tableelementsize;i++){
                        DriverObj.findElement(By.xpath("//div[@class='Cp']/div/table//tbody/tr/td[6]")).click();
                    }
                }

path of mail box might be change (you have need to change according to your work) 邮箱的路径可能已更改(您需要根据自己的工作进行更改)

Answer to 1st question : Sendkeys() method refers to user input to a particular text box on the web page where as we can also use javascriptExecuter for the same operation. 第一个问题的答案:Sendkeys()方法是指用户输入网页上的特定文本框,因为我们也可以使用javascriptExecuter进行相同的操作。

Answer to 2nd question : As per my knowledge NO. 回答第二个问题:据我所知,不是。 but I am not so sure about it. 但是我不太确定。

In Java use the following code to log in to Gmail. 在Java中,使用以下代码登录Gmail。

However I can say after clicking on sign in button the Inbox page takes time to load (at around 10 seconds max) 但是我可以说,单击登录按钮后,“收件箱”页面需要花费一些时间才能加载(最多约10秒)

So in time being if you are searching for another element without any wait (implicit or explicit) then it will throw error. 因此,如果您在没有其他等待(隐式或显式)的情况下搜索另一个元素,那么它将抛出错误。

driver.get(" http://www.gmail.com/ "); driver.get(“ http://www.gmail.com/ ”);

driver.findElement(By.id("Email")).sendKeys("userName"); driver.findElement(By.id( “电子邮件”))的SendKeys( “username” 的)。

driver.findElement(By.id("next")).click(); 。driver.findElement(By.id( “下一步”))点击();

driver.findElement(By.id("Passwd")).sendKeys("password"); driver.findElement(By.id( “的passwd”))的SendKeys( “密码”)。

driver.findElement(By.id("signIn")).click(); 。driver.findElement(By.id( “签到”))点击();

Then you can use Thread.sleep(10000) or if you have written any wait method to make the driver wait for the next object to be visible , You can use that one. 然后,您可以使用Thread.sleep(10000)或编写任何等待方法来使驱动程序等待下一个对象可见,则可以使用该对象。

Absolutely, Selenium just provides library to write automated test scripts. 绝对地,Selenium仅提供了编写自动测试脚本的库。 If you wanted to act as a human. 如果您想扮演人类。 Then you should add some wait statement in between your test steps into entire script. 然后,您应该在测试步骤之间的整个脚本中添加一些wait语句。 Until you didn't add any wait in script, it will behave like a robotic test only. 除非您没有在脚本中添加任何等待,否则它的行为就像是自动测试。 If you take any examples of test automation tool, tool doesn't add any extra information/action towards automation testing.Thats what automated testing is... It's not automatic testing. 如果您以自动化测试工具为例,该工具不会为自动化测试添加任何额外的信息/动作。这就是自动化测试的意思……不是自动化测试。 It's just computer aided testing done by machine. 这只是由机器完成的计算机辅助测试。 I hope now you'll be more clear with above example. 希望上面的例子能使您更加清楚。

另一种解决方案是,第一次尝试发送错误的电子邮件,出现错误消息后,您可以应用正确的电子邮件,这是人为的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM