【问题标题】:Why some websites are not automatable using Selenium为什么有些网站不能使用 Selenium 实现自动化
【发布时间】:2018-12-13 17:51:24
【问题描述】:

我尝试自动化 https://www.westernunion.com/global-service/track-transfer 网页,但无法弄清楚为什么网站没有导航到下一页。

我的脚本是 打开页面 -> 输入 MTCN 为 2587051083 -> 点击继续按钮 但点击后什么都没有出现。虽然手动复制相同的步骤效果很好。对于此类网站,我是否缺少任何浏览器设置?我一无所知

public static void main(String ar[]) {
        System.setProperty("webdriver.chrome.driver","D:\\Study\\selenium-java-2.48.2\\selenium-2.48.2\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://www.westernunion.com/global-service/track-transfer");
        driver.findElement(By.xpath("//input[@id='trackingNumber']")).sendKeys("2587051083");
        driver.findElement(By.xpath("//button[@id='button-track-transfer']")).click();
        }

【问题讨论】:

  • 我已经编辑了我的问题。请让我知道你的想法。另请删除您的反对票。
  • 有趣,由于某种原因它无法创建会话来获取sessionId 尝试使用firefox

标签: selenium google-chrome selenium-webdriver webdriver selenium-chromedriver


【解决方案1】:

https://www.westernunion.com/global-service/track-transfer 网页上发送跟踪字段内的字符序列 我对您自己的代码进行了一些小的修改,导致WebDriverwait 使所需的元素可点击,然后在元素上调用click(),文本为继续,如下所示:

  • 代码块:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class westernunion {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions opt = new ChromeOptions();
            opt.addArguments("start-maximized");
            opt.addArguments("disable-infobars");
            opt.addArguments("--disable-extensions");
            WebDriver driver=new ChromeDriver(opt);
            driver.get("https://www.westernunion.com/global-service/track-transfer");
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.new-field.form-control.tt-mtcn.ng-pristine.ng-valid-mask"))).sendKeys("2587051083");
            driver.findElement(By.cssSelector("button.btn.btn-primary.btn-lg.btn-block.background-color-teal.remove-margin#button-track-transfer")).click();
        }
    }
    

似乎click() 确实发生了,并且 spinner 会出现一段时间,但搜索被中断,在检查 网页 时,您会发现一些<script> 标签和 <link> 标签是指 css 有关键字 dist。举个例子:

  • <link rel="stylesheet" type="text/css" href="/content/wucom/dist/20181210075630/css/responsive_css.min.css">
  • <script src="/content/wucom/dist/20181210075630/js/js-bumblebee.js"></script>
  • <link ng-if="trackTransferVm.trackTransferData.newTrackTransfer || trackTransferVm.trackTransferData.isRetail" rel="stylesheet" type="text/css" href="/content/wucom/dist/20181210075630/css/main.min.css" class="ng-scope" style="">

这清楚地表明该网站受到 Bot Management 服务提供商 Distil Networks 的保护,并且 ChromeDriver 的导航被检测到并随后被阻止强>。


蒸馏

根据文章There Really Is Something About Distil.it...

Distil 通过观察网站行为和识别抓取工具特有的模式来保护网站免受自动内容抓取机器人的攻击。当 Distil 在一个站点上识别出恶意机器人时,它会创建一个列入黑名单的行为配置文件,并部署到其所有客户。类似于 bot 防火墙,Distil 检测模式并做出反应。

进一步,

"One pattern with Selenium was automating the theft of Web content",Distil 首席执行官 Rami Essaid 在上周接受采访时表示。 "Even though they can create new bots, we figured out a way to identify Selenium the a tool they're using, so we're blocking Selenium no matter how many times they iterate on that bot. We're doing that now with Python and a lot of different technologies. Once we see a pattern emerge from one type of bot, then we work to reverse engineer the technology they use and identify it as malicious".


参考

您可以在以下位置找到一些详细的讨论:

【讨论】:

  • 感谢您的帮助,我也可以通过脚本输入文本并单击按钮,但单击继续按钮后出现问题。你能导航到下一页吗?
  • 我正在寻找的是下一页......任何方式都非常感谢您的努力和时间。祝你有美好的一天。
  • @Nakul 查看我的答案更新并告诉我状态。
  • 我浏览了这些链接,似乎我们无法自动化那些受保护的网站。任何方式都非常感谢详细的描述/链接。
猜你喜欢
  • 2021-09-02
  • 1970-01-01
  • 2011-03-23
  • 2011-10-15
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
相关资源
最近更新 更多