【发布时间】:2019-01-22 09:48:04
【问题描述】:
使用window.performance.getEntries()检索网络数据时,我看到了不同的结果
这是代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
urls = ['https://stackoverflow.com/','https://www.google.com/']
for url in urls:
driver.get(url)
image_name = url.split(".")[1] + ".png"
driver.save_screenshot(image_name)
performance_data = driver.execute_script('return window.performance.getEntries();')
for single_data in performance_data:
file = open('Hero.txt', 'a')
files = open('Heroes.txt', 'a')
files.write(str(single_data["name"]))
if "stack" in single_data["name"]:
file.write(url + "stack_code 1")
break
if "stack" not in single_data["name"]:
file.write(url + "stack_code 0")
break
如果我删除最后一个 if 语句,我会在 Heroes.txt 中获得所有网络调用名称。因此,该代码适用于第一个 if,因为它已正确填充。如果我添加第二个if:
if "stack" not in single_data["name"]:
file.write(url + "stack_code 0")
break
我在 Heroes.txt 中得到了这个:
https://stackoverflow.com/https://www.google.com/https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.pnghttps://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.pnghttps://consent.google.com/status?continue=https://www.google.com&pc=s×tamp=1534340797&gl=GBhttps://ssl.gstatic.com/gb/images/i2_2ec824b0.pngfirst-paintfirst-contentful-painthttps://www.google.com/gen_204?s=webhp&t=aft&atyp=csi&ei=vS50W_aGHKSalwSCgrLQCQ&rt=wsrt.107,aft.119,prt.119https://www.google.com/images/nav_logo242.pnghttps://www.google.com/xjs/_/js/k=xjs.s.en_GB.LsN8oH7x4FY.O/m=sx,sb,cdos,elog,hsm,jsa,r,d,csi/am=YBZhP_4BJP-_YEBRsBWMsMAMCoZN/rt=j/d=1/dg=0/rs=ACT90oHhp5AGyfczYrjBNR_VenselWZSnAhttps://www.google.com/images/branding/product/ico/googleg_lodp.icohttps://www.google.com/xjs/_/js/k=xjs.s.en_GB.LsN8oH7x4FY.O/am=YBZhP_4BJP-_YEBRsBWMsMAMCoZN/rt=j/d=1/exm=sx,sb,cdos,elog,hsm,jsa,r,d,csi/ed=1/dg=0/rs=ACT90oHhp5AGyfczYrjBNR_VenselWZSnA/m=aa,abd,async,bgd,dvl,foot,ipv6,lu,m,mu,sf,sonic,spch,cbin,tnqaT,cbhb,xz7cCd,fEVMic,WgDvvc?xjs=s1https://www.google.com/gen_204?atyp=csi&ei=vS50W_aGHKSalwSCgrLQCQ&s=webhp&t=all&imc=3&imn=3&imp=0&adh=&conn=onchange&ima=1&ime=0&imeb=0&imeo=0&mem=ujhs.10,tjhs.14,jhsl.2330,dm.8&net=dl.10000,ect.4g,rtt.0&sys=hc.4&rt=aft.118,dcl.121,iml.118,ol.137,prt.118,xjs.297,xjsee.297,xjses.222,xjsls.138,wsrt.107,cst.15,dnst.0,rqst.81,rspt.9,sslt.13,rqstt.17,unt.1,cstt.2,dit.228&zx=1534340797851https://www.google.com/textinputassistant/tia.pnghttps://www.google.com/async/bgasy?ei=vS50W_aGHKSalwSCgrLQCQ&yv=3&async=_fmt:jspbhttps://www.google.com/xjs/_/js/k=xjs.s.en_GB.LsN8oH7x4FY.O/am=YBZhP_4BJP-_YEBRsBWMsMAMCoZN/rt=j/d=1/exm=sx,sb,cdos,elog,hsm,jsa,r,d,csi,aa,abd,async,bgd,dvl,foot,ipv6,lu,m,mu,sf,sonic,spch,cbin,tnqaT,cbhb,xz7cCd,fEVMic,WgDvvc/ed=1/dg=0/rs=ACT90oHhp5AGyfczYrjBNR_VenselWZSnA/m=RMhBfe?xjs=s2https://www.gstatic.com/og/_/js/k=og.og2.en_US.gQBLNoMk7Q0.O/rt=j/m=def/exm=in,fot/d=1/ed=1/rs=AA2YrTuPdnXARx6L0IfRJ8krP-HTrx9fswhttps://www.google.com/gen_204?atyp=i&ei=vS50W_aGHKSalwSCgrLQCQ&vet=10ahUKEwi22cnxmO_cAhUkzYUKHQKBDJoQsmQIDQ..s&zx=1534340797919https://adservice.google.com/adsid/google/uihttps://www.google.com/gen_204?atyp=i&ct=&cad=udla=3&ei=vS50W_aGHKSalwSCgrLQCQ&e=12&zx=1534340797933https://www.google.co.uk/domainless/read?igu=1https://www.google.com/js/bg/5KdFGiZjrMqKMsWhJOuJJel3qQCRBLUAy7GSORuI-sg.jshttps://apis.google.com/_/scs/abc-static/_/js/k=gapi.gapi.en.yK0z3MKtgaU.O/m=gapi_iframes,googleapis_client,plusone/rt=j/sv=1/d=1/ed=1/rs=AHpOoo-SafOYj4n3budMysbWxppU-lxJeg/cb=gapi.loaded_0https://www.google.com/domainless/write?igu=1&data=&xsrf=ALAmJdGvY5TXvkklyYKZuaWBzGhopICz3A:1534340797490https://www.google.com/gen_204?atyp=i&ct=&cad=udla=3&ei=vS50W_aGHKSalwSCgrLQCQ&pd=105&e=2&zx=1534340798039https://www.google.com/gen_204?atyp=i&ct=&cad=udla=1&ei=vS50W_aGHKSalwSCgrLQCQ&act=p&ps=2&zx=1534340798039
当我添加第二个if 后,我会在 Heroes.txt 中得到这个:
https://stackoverflow.com/https://www.google.com/
有什么想法吗?我在做傻事吗:/
【问题讨论】:
-
这里的预期结果是什么?
-
@musikreck 在添加第二个 if 语句之前得到的结果。这是所有网络调用名称。添加第二个 if 语句时,我只得到域名。因此,如果网络调用名称中有堆栈,则显示 stack_code_1,如果没有,则显示 stack_code_0。
-
为简单起见,只需使用 if-else 语句,而不是 2 个 if。另一件事,不要在
performance_data的每次迭代中都打开文件。检查performance_data是否包含任何内容,如果有,请打开文件并处理它们。 -
@musikreck 我将其更改为两个 if,因为它与 if-else 执行相同的行为。
-
所以我猜你的问题是你的不涉及 Heroes.txt 的条件语句似乎正在影响写入 Heroes.txt 的内容?
标签: python-2.7 selenium selenium-webdriver webdriver selenium-chromedriver