![](/img/trans.png)
[英]How to click on drop down menu in Python using Selenium and Chrome
[英]How to click the call drop down using selenium webdriver python?
我無法使用 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
要單擊帶有文本作為與下拉列表關聯的Call的元素,您必須誘導WebDriverWait以使元素可點擊,您可以使用以下任一解決方案:
使用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()
使用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()
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
瀏覽器快照:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.