使用Selenium Web Driver从源检索值

时间:2016-04-13 19:24:33

标签: selenium-webdriver

这是我需要使用selenium webdriver拉取PackageId值20981907的源代码。我使用时无法打印PackageId: -

JavascriptExecutor js = (JavascriptExecutor) dr;
Object val = js.executeScript("window.setPackageId;");

来源:

<script >
/* <![CDATA[ */
try{Typekit.load();}catch(e){ }
/* ]]> */

</script>
                    <script >
/* <![CDATA[ */
var adFactory = new TiiAdFactory(adConfig, "/scoop");
adFactory.setArticleId("20999784");
**adFactory.setPackageId("20981907");**  /* I need this value to be printed in eclipse console*/
adFactory.setChannel("");
adFactory.setSubchannel("scoop");
adFactory.setContentPage();
adFactory.setContentType("article");
adFactory.setParam("page", "");
adFactory.setParam("franc", "Scoop");
adFactory.setParam("subj", new Array("peoplescoop"));
adFactory.setParam("celeb", new Array("lindsaylohan"));
adFactory.setParam("sourc", source);
adFactory.setParam("appids", PEOPLE.appIds);
/* ]]> */

</script>
                    <script >
/* <![CDATA[ */
PEOPLE.News.articledate = '04-12-2016';
/* ]]> */

</script>
                    <script >
/* <![CDATA[ */
PEOPLE.adPageBannerIsEnabled=false;PEOPLE.enableStickyNav = true; PEOPLE.adHeaderPromotionIsEnabled = true;
/* ]]> */
</script>
                        <script src="/people/static/j/package/scoop/main.js"></script>
</head>

1 个答案:

答案 0 :(得分:0)

您还没有告诉我们TiiAdFactory是什么,但使用window代替实际实例肯定是错误的。 假设该类确实有一个getter,这将返回相关实例的值:

JavascriptExecutor js = (JavascriptExecutor) dr;
Object val = js.executeScript("return adFactory.getPackageId();");

我还假设您正在使用Selenium,因为您尝试从交互式用户会话中提取实时值。如果这不是真的,那么你当然可以使用任何旧的HTTP库从JavaScript源中获取值。