Home > Net >  Issue in web scraping using Selenium and driver.get()
Issue in web scraping using Selenium and driver.get()

Time:11-11

I am trying to scrape this url but the url I enter in the driver.get() gets changed when the program runs and chrome page is opened. What can be causing it to change?

I want to open this link and get specific things but the url changes and it displays error because this class doesnot exist on the changed url.

Here's my code:

s=Service(ChromeDriverManager().install())

driver = webdriver.Chrome(service=s)

driver.get("https://www.booking.com/hotel/pk/one-bahawalpur.html?aid=378266&label=bdot-Os1*aFx2GVFdW3rxGd0MYQS541115605091:pl:ta:p1:p22,563,000:ac:ap:neg:fi:tikwd-334108349:lp1011080:li:dec:dm:ppccp=UmFuZG9tSVYkc2RlIyh9YYriJK-Ikd_dLBPOo0BdMww&sid=0a2d2e37ba101e6b9547da95c4a30c48&all_sr_blocks=41645002_248999805_0_1_0;checkin=2022-11-10;checkout=2022-11-11;dest_id=-2755460;dest_type=city;dist=0;group_adults=2;group_children=0;hapos=1;highlighted_blocks=41645002_248999805_0_1_0;hpos=1;matching_block_id=41645002_248999805_0_1_0;no_rooms=1;req_adults=2;req_children=0;room1=A,A;sb_price_type=total;sr_order=popularity;sr_pri_blocks=41645002_248999805_0_1_0__1120000;srepoch=1668070223;srpvid=e9da3e27383900b4;type=total;ucfs=1&#hotelTmpl")


print(driver.find_element(by=By.CLASS_NAME, value="d2fee87262"))

The url after chrome page is opened is as follows: https://www.booking.com/searchresults.html?aid=378266&label=bdot-Os1*aFx2GVFdW3rxGd0MYQS541115605091:pl:ta:p1:p22,563,000:ac:ap:neg:fi:tikwd-334108349:lp1011080:li:dec:dm:ppccp=UmFuZG9tSVYkc2RlIyh9YYriJK-Ikd_dLBPOo0BdMww&sid=63b32ef8c1d53ae0613d71baf62c3e56&checkin=2022-11-10&checkout=2022-11-11&city=-2755460&group_adults=2&group_children=0&highlighted_hotels=416450&hlrd=with_av&keep_landing=1&no_rooms=1&redirected=1&source=hotel&srpvid=e9da3e27383900b4&room1=A,A,;#hotelTmpl

CodePudding user response:

The URL is changed by the site. You can not change its behavior. It is redirecting users with new sessions to the search instead of the page of a hotel. As a workaround I can suggest to click the hotel name in search:

driver.find_element(By.XPATH, '//div[text()="Hotel One Bahawalpur"]').click

put this line between the line that opens the page and the line that finds the element.
Also I recommend you to add a line driver.implicitly_wait(10) after the line driver = webdriver.Chrome(service=s). This will improve your scraper's stability as it makes selenium wait while an element appears on the page.

  • Related