显式等待加载面板

时间:2018-10-14 11:42:40

标签: python-3.x selenium-chromedriver webdriverwait

我具有以下网络结构

<tr class="group-head  clickable " id="date_matches-628" stage-value="14"><th colspan="5"><h3><span class="flag_16 left_16 austria_16_left">Austria - Landesliga</span></h3></th><th class="competition-link"> <a href="/national/austria/landesliga/c628/"><span>More…</span></a></th></tr>
<tr class="group-head  clickable " id="date_matches-1089" stage-value="19"><th colspan="5"><h3><span class="flag_16 left_16 austria_16_left">Austria - Jugendliga U18</span></h3></th><th class="competition-link"> <a href="/national/austria/jugendliga-u18/20182019/regular-season/r49380/"><span>More…</span></a></th></tr>

如果我单击进入每个tr节点,它将展开并加载另一个结构。像这样

<tr class="group-head  clickable  expanded loaded" id="date_matches-553" stage-value="12"><th colspan="5"><h3><span class="flag_16 left_16 austria_16_left">Austria - Regionalliga</span></h3></th><th class="competition-link"> <a href="/national/austria/regionalliga/c553/"><span>More…</span></a></th></tr>
<tr class="round-head expanded loaded"><th colspan="6"><h4><a href="/national/austria/regionalliga/20182019/mitte/r49359/">Mitte</a></h4></th></tr>
<tr class="even  expanded first last  match no-date-repetition" data-timestamp="1539518400" id="block_home_matches_29_match-2812137" data-competition="553"><td class="minute ">&nbsp;</td><td class="team team-a "><a href="/teams/austria/sv-lendorf/10948/" title="Lendorf">Lendorf</a></td><td class="score-time status"><a href="/matches/2018/10/14/austria/regionalliga/sv-lendorf/sc-weiz/2812137/?ICID=HP_MS_12_01">14 : 00</a></td><td class="team team-b "><a href="/teams/austria/sc-weiz/8729/" title="Weiz">Weiz</a></td><td class="events-button button first-occur"></td><td class="info-button button"><a href="/matches/2018/10/14/austria/regionalliga/sv-lendorf/sc-weiz/2812137/" title="Más información">Más información</a></td></tr>
<tr class="round-head expanded loaded"><th colspan="6"><h4><a href="/national/austria/regionalliga/20182019/west/r49361/">West</a></h4></th></tr>
<tr class="even  expanded first   match no-date-repetition" data-timestamp="1539518400" id="block_home_matches_29_match-2812947" data-competition="553"><td class="minute ">&nbsp;</td><td class="team team-a "><a href="/teams/austria/sv-bio-perlinger-worgl/173/" title="Wörgl">Wörgl</a></td><td class="score-time status"><a href="/matches/2018/10/14/austria/regionalliga/sv-bio-perlinger-worgl/sc-schwaz/2812947/?ICID=HP_MS_12_01">14 : 00</a></td><td class="team team-b "><a href="/teams/austria/sc-schwaz/10865/" title="Schwaz">Schwaz</a></td><td class="events-button button first-occur"></td><td class="info-button button"><a href="/matches/2018/10/14/austria/regionalliga/sv-bio-perlinger-worgl/sc-schwaz/2812947/" title="Más información">Más información</a></td></tr>
<tr class="odd  expanded    match no-date-repetition" data-timestamp="1539518400" id="block_home_matches_29_match-2812959" data-competition="553"><td class="minute ">&nbsp;</td><td class="team team-a "><a href="/teams/austria/fc-red-bull-salzburg-amateure/3367/" title="Anif / Salzburgo II">Anif / Salzbur…</a></td><td class="score-time status"><a href="/matches/2018/10/14/austria/regionalliga/fc-red-bull-salzburg-amateure/svg-reichenau/2812959/?ICID=HP_MS_12_02">14 : 00</a></td><td class="team team-b "><a href="/teams/austria/svg-reichenau/15753/" title="Reichenau">Reichenau</a></td><td class="events-button button first-occur"></td><td class="info-button button"><a href="/matches/2018/10/14/austria/regionalliga/fc-red-bull-salzburg-amateure/svg-reichenau/2812959/" title="Más información">Más información</a></td></tr>

因此,我要做的是以下几点。首先,我搜索“父”节点,如果class属性不包含“扩展加载”,请点击它。

nodo=browser.find_element_by_xpath('.//tr[@id="{}"]'.format(ids))
if 'expanded loaded' not in league.get_attribute('class'):

try: league.click()
except WebDriverException:
    try:
        print('contingencia click')
        browser.execute_script("arguments[0].click();", league)

问题,有时在单击我要获取的节点后,它们没有完全加载,就像

<tr class=" loading event"></tr>

所以当我执行以下操作来搜索它们

partido=league.find_element_by_xpath('./following-sibling::tr[@class and not(contains(@class,"loading"))]')

我得到的是要单击的下一个节点,而不是应该加载的节点。 因此,简而言之,我该如何设置显式等待,以使程序而不是转到下一个非加载节点,而等待下一个节点更改

class=" loading event"

<tr class="even  expanded first   match no-date-repetition" data-timestamp="1539518400" id="block_home_matches_29_match-2812947" data-competition="553"><td class="minute ">&nbsp;</td><td class="team team-a "><a href="/teams/austria/sv-bio-perlinger-worgl/173/" title="Wörgl">Wörgl</a></td><td class="score-time status"><a href="/matches/2018/10/14/austria/regionalliga/sv-bio-perlinger-worgl/sc-schwaz/2812947/?ICID=HP_MS_12_01">14 : 00</a></td><td class="team team-b "><a href="/teams/austria/sc-schwaz/10865/" title="Schwaz">Schwaz</a></td><td class="events-button button first-occur"></td><td class="info-button button"><a href="/matches/2018/10/14/austria/regionalliga/sv-bio-perlinger-worgl/sc-schwaz/2812947/" title="Más información">Más información</a></td></tr>

谢谢

1 个答案:

答案 0 :(得分:0)

您的网站似乎正在使用Angular或类似的语言...

所以我建议要做的是注入JavaScript ...

我为此写了一个函数,希望对您有帮助...

from datetime import datetime, timedelta
from selenium.common.exceptions import WebDriverException
import time


def wait_until_angular(driver, seconds: int = 10) -> None:
    java_script_to_load_angular = "var injector = window.angular.element('body').injector(); " \
                              "var $http = injector.get('$http');" \
                              "return ($http.pendingRequests.length === 0);"
    end_time = datetime.utcnow() + timedelta(seconds=seconds)
    print("wait for Angular Elements....")
    while datetime.utcnow() < end_time:
        try:
            if driver.execute_script(java_script_to_load_angular):
                return
        except WebDriverException:
            continue
        time.sleep(0.1)
    raise TimeoutError("waiting for angular elements for too long")

您可以根据需要在代码中实现它!

您可以阅读有关execute_script here的更多信息。

希望这会有所帮助!