1

I've written a script in python in combination with selenium to initiate click on some links available in a webpage. They are all very similar to hperlinks. The links I wanna click on are the name of each container, as in the first link connected to this name is Kimpton Muse Hotel (the name may vary in your case) and so on. I've already examined that my script can parse those names but can't click on those links connected to each name.

The webpage link

This is my try so far:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = "replace with the link above"

driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver, 10)

for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".VkpGBb .dbg0pd div"))):
    # item.click()  #it throws an error when uncommented
    print(item.text)  #this works great
driver.quit()

This is the error I get when uncomment the line item.click():

line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <div>...</div> is not clickable at point (106, 238). Other element would receive the click: <div jstcache="360" jsaction="cal_select_day:jsl._" class="gws-travel-calendar__calendar">...</div>
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86)

What I wish to do is click on those links cyclically and that's it.

The following image shows the links I wish to click on (marked by pencil marker)

enter image description here

5
  • Do these links appear within an iframe or something? Commented Aug 1, 2018 at 17:23
  • Nope. They are not @SuperStew . Commented Aug 1, 2018 at 17:26
  • The calendar widget which is currently hidden overlaps the area where you're trying to click. I'm not sure the right way to handle that yet, but that's what's causing your error. Go ahead and expand the calendar widget manually and you can see the area it's overlapping. Commented Aug 1, 2018 at 17:38
  • Did you try to click on <a class="C8TUKc iVV2dCH7jxU0-6WH35iSZ2V0 rllt__link a-no-hover-decoration" element instead of <div? Commented Aug 1, 2018 at 17:44
  • It seems you are close @Kiril S. Your solution works only when I kick out calendar widget. However, it is still incomplete approach. Commented Aug 1, 2018 at 18:00

2 Answers 2

2

Try to close Details modal popup on each iteration:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.CngMb g-raised-button.bZEQvc"))).click()
for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".VkpGBb .dbg0pd div"))):
    item.click()
    name = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"div.xpdopen div.SPZz6b span")))
    print(name.text)
    close = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "QU77pf")))
    driver.execute_script("arguments[0].click();", close)
Sign up to request clarification or add additional context in comments.

Comments

1

There is one calendar windows opens up , once we visit the search page.You can click on the Finish button and run the same piece of code :

Here how I have done it :

driver.maximize_window()

driver.get("https://www.google.com.pk/search?q=motels+in+new+jersey&npsic=0&rflfq=1&rlha=1&rlla=0&rlhsc=CiEIjc2myO-zjdfsAQjm4uekqp_1vYMBCJ2lpoWkg62PtwEwAQ&rllag=39956030,-74419977,66425&tbm=lcl&ved=2ahUKEwi48eTj-MncAhVG4YUKHXUPA0QQjGp6BAgGEFY&hotel_dates=2018-08-12,2018-08-13&tbs=lf_hd:-1,lf_ho:2,lrf:!1m4!1u13!2m2!13m1!1b1!1m4!1u10!2m2!11m1!1e4!1m4!1u10!2m2!11m1!1e9!1m4!1u10!2m2!11m1!1e8!1m4!1u10!2m2!11m1!1e10!1m4!1u10!2m2!11m1!1e2!1m4!1u10!2m2!11m1!1e3!1m4!1u10!2m2!11m1!1e7!2m1!1e13!2m7!1e17!4m2!17m1!1e3!4m2!17m1!1e8!2m1!1e2!2m1!1e4!2m22!1e10!4m2!11m1!1e4!4m2!11m1!1e9!4m2!11m1!1e8!4m2!11m1!1e10!4m2!11m1!1e2!4m2!11m1!1e3!4m2!11m1!1e7!2m21!1e7!4m4!7m3!1m1!1u20000!3sPKR!4m4!7m3!1m1!1u30000!3sPKR!4m4!7m3!1m1!1u40000!3sPKR!4m4!7m3!1m1!1u50000!3sPKR!3sIAE,lf:1,lf_ui:6&rldoc=1#rlfi=hd:2018-08-12,2018-08-13;si:;mv:!1m3!1d946339.8611287393!2d-74.7837865!3d40.2277793!2m3!1f0!2f0!3f0!3m2!1i146!2i233!4f13.1;tbs:lf_hd:-1,lf_ho:2,lrf:!1m4!1u13!2m2!13m1!1b1!1m4!1u10!2m2!11m1!1e4!1m4!1u10!2m2!11m1!1e9!1m4!1u10!2m2!11m1!1e8!1m4!1u10!2m2!11m1!1e10!1m4!1u10!2m2!11m1!1e2!1m4!1u10!2m2!11m1!1e3!1m4!1u10!2m2!11m1!1e7!2m21!1e7!4m4!7m3!1m1!1u20000!3sPKR!4m4!7m3!1m1!1u30000!3sPKR!4m4!7m3!1m1!1u40000!3sPKR!4m4!7m3!1m1!1u50000!3sPKR!2m1!1e2!2m1!1e4!2m1!1e13!2m22!1e10!4m2!11m1!1e4!4m2!11m1!1e9!4m2!11m1!1e8!4m2!11m1!1e10!4m2!11m1!1e2!4m2!11m1!1e3!4m2!11m1!1e7!2m7!1e17!4m2!17m1!1e3!4m2!17m1!1e8!3sIAE,lf:1,lf_ui:6") 

wait = WebDriverWait(driver,30)

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Finished']"))).click()

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".VkpGBb .dbg0pd div")))

for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".VkpGBb .dbg0pd div"))):
 item.click()  #it throws an error when uncommented
 print(item.text)  #this works great  

Imports would be :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC   

Here is the console output :

WestHouse Hotel New York
Kimpton Muse Hotel
Motel 6 East Brunswick
Motel 6 Piscataway
Baymont by Wyndham Atlantic City Madison Hotel
Super 8 by Wyndham Mahwah
Super 8 by Wyndham Mount Laurel
SuperLodge Atlantic City/Absecon
Knights Inn Mount Laurel
Empire Inn & Suites - Atlantic City/Absecon
Horizon Inn
Super 8 by Wyndham Pennsville/Wilmington
Anchor Motel
Knights Inn South Amboy/Garden State Parkway South Exit 125
Studio 6 East Brunswick - NYC Area
Super 8 by Wyndham North Bergen NJ/NYC Area
Hudson Plaza Motel
Motel 6 Elizabeth - Newark Liberty International Airport
Motel 6 Philadelphia Pa - Brooklawn NJ
Travelodge by Wyndham Absecon Atlantic City
Motel 6 New Brunswick NJ
Travelodge by Wyndham Parsippany  

Update 1 :

Wrap the Finished button clicking in try-catch :

try:  
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Finished']"))).click()

except:
   pass

7 Comments

It didn't work. Script thows timeout exception error upon hitting the line where you've defined xpath.
Can you share the stack trace : It is working fine at my end though.
Did calendar open in your system ? I have shared the console output also , note that I am printing after clicking on each link.
Check this out: Traceback (most recent call last): File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\demo.py", line 33, in <module> wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Finished']"))).click() File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
Did calendar open in your system ?
|

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.