[英]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?我无法使用 xpath 或 css 选择器单击呼叫下拉列表并抓取电话号码。有什么办法吗?
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:要单击带有文本作为与下拉列表关联的Call的元素,您必须诱导WebDriverWait以使元素可点击,您可以使用以下任一解决方案:
Using CSS_SELECTOR
:使用
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
:使用
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:浏览器快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.