0

I try access to results home of Manchester United but impossible to access. .Here is the website page

And here is below a piece of code HTML the page:

<div id="g_1_l2dtbMED" title="Click for match detail!" class="event__match event__match--static event__match--last event__match--oneLine"><div class="event__time">04.10. 17:30</div><div class="event__participant event__participant--home"><svg class="card___2ip_DLm icon--redCard icon--redCard-first icon--redCard-last"><title></title><use xlink:href="/res/_fs/build/symbols.f1bc6b2.svg#card"></use></svg>Manchester Utd</div><div class="event__scores fontBold"><span>1</span>&nbsp;-&nbsp;<span>6</span></div><div class="event__participant event__participant--away fontBold">Tottenham</div><div class="event__part">(1&nbsp;-&nbsp;4)</div><span class="wld wld--l" title="Loss">L</span></div>

The results that I to search parse is 'L' and is located in balise <span>.

Here is the code that I did for try parse it:

driver = webdriver.Chrome()
url = "https://www.flashscore.com/team/manchester-united/ppjDR086/results/"
driver.get(url)

Team = 'manchester Utd'
results = WebDriverWait(driver, 20).until(EC.find_elements((By.XPATH,"//div[@class='event__participant--home' and contains(text(),'"+ Team +"')]//ancestor::div/span")))
print(len(results))

But this cast me exception 'TimeoutException' after 20s that is time limit of search.

9
  • //div[@class='event__participant--home'] typo? Commented Oct 16, 2020 at 0:09
  • No, it's done on purpose because the result must be the brother of balise that contained this name class and the text 'Team'. it must fill double condition. This not function find_elements() that I use but visibility_of_element_located() Commented Oct 16, 2020 at 0:22
  • 'manchester Utd' should be 'Manchester Utd' Commented Oct 16, 2020 at 0:26
  • also find_elements should be an expected conditions. Commented Oct 16, 2020 at 0:26
  • 1
    EC.presence_of_all_elements_located the condition for what you are waiting for, Commented Oct 16, 2020 at 0:40

2 Answers 2

1

The locator you are looking for is

//div[contains(@class,'event__participant--home')][text()='Manchester Utd']//following-sibling::span[1]
^ find a DIV that contains the class indicating a home game
                                                  ^ that also contains the team name
                                                                           ^ then find the first sibling SPAN that follows

That locator will find the elements that contain L, W, D, etc. for only the home games.

If you are going to wait for the elements, you will want to wait for visible, not presence. Presence is when the element is just in the DOM but not necessarily visible. If you are going to scrape text off the page, you need to wait for visible. You can do that using EC.visibility_of_all_elements_located(). See the docs. If you try to scrape the page when they are present but not visible, it will throw an exception.

Your updated code is below

driver = webdriver.Chrome()
url = "https://www.flashscore.com/team/manchester-united/ppjDR086/results/"
driver.get(url)

Team = 'Manchester Utd'
results = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[contains(@class,'event__participant--home')][text()='" + Team + "']//following-sibling::span[1]")))
print(len(results))
Sign up to request clarification or add additional context in comments.

Comments

1

try this xpath-

//div[contains(text(),'Manchester Utd')]/following-sibling::span

1 Comment

Excuse me this do not solve my problem because this code takes all results of Man.utd . Result both away and at home.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.