I'm using selenium in python and trying to click the "See all Properties" button to get to the next web page where all the properties will be listed and I can easily scrap the data.
Here's what the situation looks like
This is the site: https://www.magicbricks.com/
The element of the button is shown:
<a href="javascript:void(0);" class="mb-home__section__title--anchor-see-all push-right" onclick="fireDynamicPropSeeAllGTM(event,'Owner properties see all','ownerProp');seeAppPropertiesOpenUrl('ownerProp');">
See all Properties
</a>
Right now my script just selects the action(buy or rent etc.) and selects the city the user wants to view properties in. After that I have to make selenium click on the "See all Properties" button so that I can get a proper list of properties, which will be easier to scrap.
Now, here is how I tried to solve the problem:
try:
#Get the element
wait = WebDriverWait(driver, 10)
see_all_properties = wait.until(
EC.visibility_of_element_located((By.XPATH, "/html/body/div[5]/div[1]/div[10]/div/section/div[1]/a"))#XPATH from browser inspect menu
)
#Get the original opened tab
original_tab = driver.current_window_handle
#Click the button
driver.execute_script("arguments[0].click();", see_all_properties)
print("->Successfully clicked the button using JavaScript.")
print("->Seeing all popular properties in your area...")
#Wait for the tab
wait = WebDriverWait(driver, 10)
wait.until(EC.number_of_windows_to_be(2))
#Loop through all the tabs and make the new tab primary
for tab in driver.window_handles:
if tab != original_tab:
driver.switch_to.window(tab)
break
except Exception as e:
print(f"->Error: {e}")
And these are the errors I get:
--Enter any of the actions(case-sensitive) - ['Buy', 'Rent', 'PG', 'Plot', 'Commercial', 'Post Free Property Ad']: Buy
--Enter your state: Chandigarh
->Hovering over state selector element...
->Successfully clicked on Chandigarh.
->Error: Message:
Stacktrace:
#0 0x59391a14cfba <unknown>
#1 0x593919bd16d0 <unknown>
#2 0x593919c232aa <unknown>
#3 0x593919c23541 <unknown>
#4 0x593919c716c4 <unknown>
#5 0x593919c48e5d <unknown>
#6 0x593919c6eb54 <unknown>
#7 0x593919c48c03 <unknown>
#8 0x593919c157a8 <unknown>
#9 0x593919c16421 <unknown>
#10 0x59391a111b28 <unknown>
#11 0x59391a11587f <unknown>
#12 0x59391a0f9c49 <unknown>
#13 0x59391a116405 <unknown>
#14 0x59391a0df4ff <unknown>
#15 0x59391a13a258 <unknown>
#16 0x59391a13a432 <unknown>
#17 0x59391a14bfa3 <unknown>
#18 0x7218daea27f1 <unknown>
#19 0x7218daf33b5c <unknown>
Note that I have already tried:
Checking my web driver version
ActionChain clicking method
Basic driver.get_element().click() method
Switching tabs so that selenium doesn't give error when there are two tabs
When I click that button manually through my browser, a new tab opens which contains the contents I want to scrap.
Any help would be extremely appreciated.
minimal working codeso everyone could simply copy and test it, and use it for modifications (to test some solutions)--headlessto see what browser is doing?see_all_properties.click()without usingjavascript"arguments[0].click();"also works for me - but only on Chrome. Firefox has some problem.