我有以下网络结构
<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 "> </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 "> </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 "> </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>
所以,我做的是以下几点。首先,我搜索"父"节点,如果类属性不包含"扩展加载"y,请单击它。
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 "> </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>
谢谢
看起来您的网站正在使用 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的更多信息。
希望这有帮助!