简体   繁体   中英

How to click the call drop down using selenium webdriver python?

I am not able to click the call dropdown using xpath or css selector and scrape the phone numbers.Is there any way to do so?

driver=set_options_driver(headless=False)
driver.get('https://www.yellowpages.ca/bus/Ontario/Maidstone/CSR-Coxon-s-Sales-Rentals-Ltd/7222758.html?what=Rajic-Holdings-Inc&where=Maidstone+ON&useContext=true')
yp_name=[k.text for k in driver.find_elements_by_xpath("//div[@class='merchant__info--root ']//div[@class='merchant__name']")]
yp_addr=[k.text for k in driver.find_elements_by_xpath("//div[@class='merchant__info--root ']//div[@class='merchant__item merchant__address merchant__address__mobile']")]
try:
    #driver.find_element_by_xpath("//div[@class='merchant__info-content']//span[@class='ypicon ypicon-phone mlr__icon']").click()
    driver.find_element_by_xpath("//ul[@class='mlr mlr--merchant']//span[@class='ypicon ypicon-phone mlr__icon']").click()
    yp_phone=driver.find_element_by_xpath("//li[@class='mlr__item mlr__item--more mlr__item--phone mlr__item--active isActive']//span[@class='mlr__sub-text']").text
except:
    yp_phone=" "

您可以尝试单击如下所示的锚标记,而不是单击 span 元素。

driver.find_element_by_xpath("//ul[@class='mlr mlr--merchant']/li/a").click

To click on the element with text as Call associated with the dropdown you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR :

     driver.get("https://www.yellowpages.ca/bus/Ontario/Maidstone/CSR-Coxon-s-Sales-Rentals-Ltd/7222758.html?what=Rajic-Holdings-Inc&where=Maidstone+ON&useContext=true") WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.merchant__info--root span.ypicon.ypicon-phone.mlr__icon"))).click()
  • Using XPATH :

     driver.get("https://www.yellowpages.ca/bus/Ontario/Maidstone/CSR-Coxon-s-Sales-Rentals-Ltd/7222758.html?what=Rajic-Holdings-Inc&where=Maidstone+ON&useContext=true") WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='merchant__info--root ']//span[@class='ypicon ypicon-phone mlr__icon']"))).click()
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:

黄页

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