简体   繁体   中英

Python selenium script fails to click on some links

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)

在此输入图像描述

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM