Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium在站点中搜索跟踪脚本_Selenium - Fatal编程技术网

使用Selenium在站点中搜索跟踪脚本

使用Selenium在站点中搜索跟踪脚本,selenium,Selenium,我刚刚开始研究硒元素,所以这可能是一个有点不确定的问题。我的一位同事必须检查我们网站的大约100页,看看是否有跟踪脚本插入其中。她必须每月做一次,因此我决定研究硒 目前,她进入页面查看源代码并搜索此(以及其他脚本跟踪功能) 有些页面包含需要填写和提交的表格。我知道如何使用selenium IDE轻松地设置dom元素的值 我不确定如何做的是告诉selenium在源代码中搜索此脚本。如果需要,我可以在服务器上安装SeleniumWebDriver。我以前没有用Java、C#或Python编写过程

我刚刚开始研究硒元素,所以这可能是一个有点不确定的问题。我的一位同事必须检查我们网站的大约100页,看看是否有跟踪脚本插入其中。她必须每月做一次,因此我决定研究硒

目前,她进入页面查看源代码并搜索此(以及其他脚本跟踪功能)


有些页面包含需要填写和提交的表格。我知道如何使用selenium IDE轻松地设置dom元素的值

我不确定如何做的是告诉selenium在源代码中搜索此脚本。如果需要,我可以在服务器上安装SeleniumWebDriver。我以前没有用Java、C#或Python编写过程序,但我确实在php中安装了node.js

有什么建议吗?

在Java中:

String trackingScript = "<script type="text/javascript" src="https://ssltracking.esearchvision.com/esi/trackingtest.js"></script>"

String html = driver.getPageSource();
if (html.indexOf(trackingScript) != -1) {
    // the script is in the page source
}
else {
    // the script is not in the page source
}
String trackingScript=“”
字符串html=driver.getPageSource();
if(html.indexOf(trackingScript)!=-1){
//脚本位于页面源代码中
}
否则{
//该脚本不在页面源中
}

在python中,使用selenium元素定位器而不是字符串匹配:

from selenium import selenium
import selenium.webdriver.support.ui as ui
wait = ui.WebDriverWait(driver,10)
driver = webdriver.Firefox() ## or whatever browser you prefer
driver.get("yourpage.com")
## wait for page to load
wait.until(lambda driver: driver.find_elements_by_tag_name('script') )
scripts = driver.find_elements_by_tag_name('script')

for script in scripts:
    if( script.get_attribute('src') == \
        'https://ssltracking.esearchvision.com/esi/trackingtest.js' and \
        script.get_attribute('type') == 'text/javascript' ):
        ## do whatever

我想你要找的是从网页上删除源代码。然后以某种方式美化它,最后搜索脚本标记。我推荐Ruby作为编程语言,nokogiri gem作为实现这一点的最简单方法。字符串匹配可能不是很健壮,我认为使用元素定位器会更好
from selenium import selenium
import selenium.webdriver.support.ui as ui
wait = ui.WebDriverWait(driver,10)
driver = webdriver.Firefox() ## or whatever browser you prefer
driver.get("yourpage.com")
## wait for page to load
wait.until(lambda driver: driver.find_elements_by_tag_name('script') )
scripts = driver.find_elements_by_tag_name('script')

for script in scripts:
    if( script.get_attribute('src') == \
        'https://ssltracking.esearchvision.com/esi/trackingtest.js' and \
        script.get_attribute('type') == 'text/javascript' ):
        ## do whatever