首页 > 解决方案 > 使用 python 将 if 或 else 添加到 selenium google webdriver 时得到不同的结果

问题描述

使用检索网络数据时,我看到了不同的结果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&timestamp=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/

有任何想法吗?我在做傻事吗:/

标签: python-2.7seleniumselenium-webdriverwebdriverselenium-chromedriver

解决方案


怎么了:

当您插入第二个if条件时,您的代码编写方式不同的原因是有时满足第二个条件,因此break从您的for循环中迭代performance_data。如果您删除了第二个if条件,那么 when'stack' not in single_data["name"]将不存在break,您将继续for循环的下一次迭代并继续写入Heroes.txt.

理解事物:

当您添加第二if条语句时,您只有 2 个值写入Heroes.txt,因为您可以保证 abreak来自for循环,但您正在迭代 2 个 URL。

当您删除第二条if语句时,并不总是保证 abreakfor循环中,因此(通常)为您提供更多写入Heroes.txt.

建议:

这是一个逻辑问题,而不是 Selenium 或 Python 问题。如果您可以告诉我们您的预期输出是什么,或者您希望将什么写入文件,我们可以帮助您构建代码以实现该预期。


根据我的猜测,您可能想要这样的东西(我已经删除了一些在这篇文章中未使用的代码):

from selenium import webdriver

driver = webdriver.Chrome()

urls = ['https://stackoverflow.com/','https://www.google.com/']

for url in urls:
    driver.get(url)
    performance_data = driver.execute_script('return window.performance.getEntries();')

    pass_flag = False

    for single_data in performance_data:
        # As opposed to breaking or continuing, we're just going to pass over
        # to the next bit of code where we write to Heroes.txt after we've
        # written to Hero.txt once per URL
        if pass_flag:
            pass 
        else:
            if 'stack' in single_data['name']:
                file = open('Hero.txt', 'a')
                file.write(url + 'stack_code 1')
            if 'stack' not in single_data['name']:
                file = open('Hero.txt', 'a')
                file.write(url + 'stack_code 0')
            pass_flag = True 

        # Unlike the above, we're *always* going to write to Heroes.txt
        files = open('Heroes.txt', 'a')
        files.write(str(single_data['name']))

结果来自Hero.txt

https://stackoverflow.com/stack_code 1https://www.google.com/stack_code 0

结果来自Heroes.txt

https://stackoverflow.com/https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.jshttps://cdn.sstatic.net/Js/stub.en.js?v=c8d9d8cf9f8ahttps://cdn.sstatic.net/Shared/stacks.css?v=bfa43f7a5209https://cdn.sstatic.net/Sites/stackoverflow/primary-unified.css?v=ebcfa5178eebhttps://i.stack.imgur.com/tKsDb.pnghttps://i.stack.imgur.com/5d55j.pnghttps://i.stack.imgur.com/xKsQb.pnghttps://i.stack.imgur.com/NT6zO.pnghttps://i.stack.imgur.com/9J8MK.pnghttps://i.stack.imgur.com/r0io0.pnghttps://i.stack.imgur.com/EQnZM.pnghttps://i.stack.imgur.com/8UH0j.pnghttps://cdn.sstatic.net/Img/unified/sprites.svg?v=e5e58ae7df45https://i.stack.imgur.com/817gJ.pnghttps://cdn.sstatic.net/img/hero/pattern.svg?v=13ca42e6c21dfirst-paintfirst-contentful-painthttps://cdn.sstatic.net/clc/clc.min.js?v=04d772c81312https://www.googletagservices.com/tag/js/gpt.jshttps://cdn.sstatic.net/img/favicons-sprite16.png?v=08c43b9ad64chttps://www.google-analytics.com/analytics.jshttps://secure.quantserve.com/quant.jshttps://sb.scorecardresearch.com/beacon.jshttps://cdn.sstatic.net/Js/full-anon.en.js?v=5f2ad1ab6af9https://adservice.google.com/adsid/integrator.js?domain=stackoverflow.comhttps://securepubads.g.doubleclick.net/gpt/pubads_impl_239.jshttps://cdn.sstatic.net/clc/styles/clc.min.css?v=f28c677e209bhttps://stackoverflow.com/gps/eventhttps://rules.quantcount.com/rules-p-c1rF4kxgLUzNc.jshttps://www.google-analytics.com/r/collecthttps://sb.scorecardresearch.com/b?c1=2&c2=17440561&ns__t=1534347714086&ns_c=UTF-8&cv=3.1&c8=Stack%20Overflow%20-%20Where%20Developers%20Learn%2C%20Share%2C%20%26%20Build%20Careers&c7=https%3A%2F%2Fstackoverflow.com%2F&c9=https://pixel.quantserve.com/pixel;r=767658432;rf=0;a=p-c1rF4kxgLUzNc;url=https%3A%2F%2Fstackoverflow.com%2F;fpan=1;fpa=P0-551429989-1534347714162;ns=0;ce=1;qjs=1;qv=4c19192-20180628134937;cm=;ref=;je=0;sr=2048x1152x24;enc=n;dst=1;et=1534347714161;tzo=420;ogl=type.website%2Curl.https%3A%2F%2Fstackoverflow%252Ecom%2F%2Csite_name.Stack%20Overflow%2Cimage.https%3A%2F%2Fcdn%252Esstatic%252Enet%2FSites%2Fstackoverflow%2Fimg%2Fapple-touch-icon%402%252Epng%3Fv%3D73d79a8%2Ctitle.Stack%20Overflow%20-%20Where%20Developers%20Learn%252C%20Share%252C%20%26%20Build%20Careers%2Cdescription.Stack%20Overflow%20%7C%20The%20World%E2%80%99s%20Largest%20Online%20Community%20for%20Developershttps://securepubads.g.doubleclick.net/gpt/pubads_impl_rendering_239.jshttps://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43dhttps://www.google.com/https://www.google.com/images/hpp/shield_privacy_checkup_green_2x_web_96dp.pnghttps://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.pnghttps://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.pnghttps://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu4mxK.woff2https://fonts.gstatic.com/s/roboto/v18/KFOlCnqEu92Fr1MmWUlfBBc4.woff2https://ssl.gstatic.com/gb/images/i2_2ec824b0.pngfirst-paintfirst-contentful-painthttps://www.google.com/gen_204?s=webhp&t=aft&atyp=csi&ei=yEl0W7-oHojz0wLly6CICA&rt=wsrt.359,aft.315,prt.315https://www.google.com/xjs/_/js/k=xjs.s.en_US.R6OYO7uDOhg.O/m=sx,sb,cdos,elog,hsm,jsa,r,d,csi/am=YBZhP_4BJP-_YEChYCsYYYEZFAyb/rt=j/d=1/dg=0/rs=ACT90oH8mcRSbgCKJPsRvB4s5dlgzuOWqg  

我遵循您在上述解决方案中的程序中使用的相同结构。以下是我会做的方式:

from selenium import webdriver

driver = webdriver.Chrome()

urls = ['https://stackoverflow.com/','https://www.google.com/']

for url in urls:
    driver.get(url)
    performance_data = driver.execute_script('return window.performance.getEntries();')

    pass_flag = False

    for single_data in performance_data:
        if pass_flag:
            pass 
        else:
            # Using with...as guarantees that the file is closed; also, we 
            # didn't have to rewrite 1 line of code
            with open('Hero.txt', 'a') as file:  
                if 'stack' in single_data['name']:
                    file.write(url + 'stack_code 1')
                else:
                    file.write(url + 'stack_code 0')
            pass_flag = True 

        # As above, using with...as guarantees that the file is closed
        with open('Heroes.txt', 'a') as files:
            files.write(str(single_data['name']))

推荐阅读