selenium’s driver gets wrong page in python

I am trying to scrape certain odds for a football tournament. To this end I wrote a piece of code which first generates the exact link I want and then loads the corresponding page. The problem is, the page loaded is not the exact page but a ‘neighbor’ (see below). Here is the code:

from selenium import webdriver
from bs4 import BeautifulSoup
import re

driver = webdriver.Chrome()
base_url = 'https://www.oddsportal.com/football/germany/bundesliga/' # next games for bundesliga
base_site = 'https://www.oddsportal.com'
team1 = 'dortmund' 

driver.get(base_url)
soup = BeautifulSoup(driver.page_source, 'html.parser')

# match_tags contains references to dortmund's upcoming games
match_tags = soup.find_all('a', {'href':re.compile('dortmund')})
assert len(match_tags) == 2, 'Unexpected number of tags found' # we expect length 2 here

relevant_tag = match_tags[0]

# having found the relevant information, we now create the second link and try loading the page
game_href = relevant_tag['href']

game_url = base_site + game_href + "#cs;2"
# we should get this link: https://www.oddsportal.com/football/germany/bundesliga/dortmund-heidenheim-zsmd0ggn/#cs;2
assert game_url == 'https://www.oddsportal.com/football/germany/bundesliga/dortmund-heidenheim-zsmd0ggn/#cs;2', 'Unexpected link'

driver.get(game_url)
soup = BeautifulSoup(driver.page_source, 'lxml-xml')
# print(soup.prettify())

# the following command shows that the loaded page is not the correct one. For the correct one it should return a long list, however it returns an empty list upon running
print(soup.find_all("p", {"data-v-962b58dc" : True})) 

This code runs just fine. The problem is, it loads a wrong page! In fact it loads https://www.oddsportal.com/football/germany/bundesliga/dortmund-heidenheim-zsmd0ggn instead of https://www.oddsportal.com/football/germany/bundesliga/dortmund-heidenheim-zsmd0ggn/#cs;2 (notice ‘/#cs;2’ at the end missing). The above code represents a somewhat minimal example: if I directly give the driver the page https://www.oddsportal.com/football/germany/bundesliga/dortmund-heidenheim-zsmd0ggn/#cs;2 to load without previously loading https://www.oddsportal.com/football/germany/bundesliga , it gets it correctly.

Comments. I have tried using webdriver.Firefox() instead of Chrome, with the same result. The code print(driver.current_url) shows the correct link. Fun fact: running the last two command again,

driver.get(game_url)
soup = BeautifulSoup(driver.page_source, 'lxml-xml')

loads the correct page!

Edit This is to answer the question how do we know that the loaded page is not correct. The correct page has the specific structure and print(soup.find_all("p", {"data-v-962b58dc" : True})) returns a long list of tags related to specific results. The wrong page returns empty list upon executing this command (this is just one way, it’s also possible to see that the page is wrong by printing out the source).

2

Try waiting for the page to load fully, i think it might be cause of the fragment identifier (the part after #) is usually processed by javascript after the main page is loaded or use execute_script in driver and to navigate directly.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật