Selenium无法定位从按按钮打开的小窗口中的元素。

huangapple go评论64阅读模式
英文:

Selenium cannot locate an element in a small window opened from pressing a button

问题

我正在尝试从一个网站上提取数字,但Selenium在最后一部分遇到了一些问题。

在进入诊所的个人资料后,我希望代码点击这部分,点击后会弹出一个包含号码的小窗口。

这个窗口里的号码是一个具有类名“fs-text-2 whatsapp-click-stats”的WhatsApp链接。

以下是我正在使用的代码片段:

url = "https://infoguia.com/bnom.asp?txt=clinicas&est=100"

# 创建一个新的Chrome会话
driver = webdriver.Chrome(r'C:\Users\hamxa\Desktop\Chromdriver\chromedriver.exe')
driver.implicitly_wait(50)
driver.get(url)

# 获取页面上诊所的列表
clinics = driver.find_elements_by_class_name("grey-btn-profile.fs-text-button-1.fw-bold")

# 循环遍历每个诊所以获取其电话号码
for clinic in clinics:
  clinic.click() #打开每个诊所的详细页面 
  try:
    whatsapp_button = driver.find_element_by_id('view-whatsapp')
    whatsapp_button.click()
    
    # 使用类名“fs-text-2 whatsapp-click-stats”查找号码
    number = driver.find_element_by_class_name('fs-text-2 whatsapp-click-stats')
    # 打印号码
    print(number.text)
  finally:
    driver.back() # 返回到搜索结果页面

# 关闭webdriver
driver.close()

我遇到的错误是:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".fs-text-2 whatsapp-click-stats"}

有关如何解决这个问题的任何想法?

英文:

I'm trying to scrape numbers from a website and Selenium is having some trouble with the final part

Selenium无法定位从按按钮打开的小窗口中的元素。

After going into the profile of a clinic, I want the code to click this part, after this part is clicked a small window opens up with the number inside.

Selenium无法定位从按按钮打开的小窗口中的元素。

the number in this window is a whatsapp link with the class "fs-text-2 whatsapp-click-stats"

following is the snippet of the code I'm using

url = "https://infoguia.com/bnom.asp?txt=clinicas&est=100"

# create a new Chrome session
driver = webdriver.Chrome(r'C:\Users\hamxa\Desktop\Chromdriver\chromedriver.exe')
driver.implicitly_wait(50)
driver.get(url)

# get list of clinics on the page
clinics = driver.find_elements_by_class_name("grey-btn-profile.fs-text-button-1.fw-bold")

# loop through each clinic to get its phone number
for clinic in clinics:
  clinic.click() #open detail page for each clinic 
  try:
    # element = WebDriverWait(driver, 10).until(
    #     EC.presence_of_element_located((By.CLASS_NAME, "fs-text-2.phone-click-stats"))
    # )
    
    whatsapp_button = driver.find_element_by_id('view-whatsapp')
    
    whatsapp_button.click()
    
    # find the number with the class "fs-text-2 whatsapp-click-stats"
    number = driver.find_element_by_class_name('fs-text-2 whatsapp-click-stats')
    # print the number
    print(number.text)
  finally:
    driver.back() # go back to the search results page
    
# close the webdriver
driver.close()

and this is the error i'm getting:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".fs-text-2 whatsapp-click-stats"}

any ideas on how to resolve this?

答案1

得分: 1

你可以尝试这样做,

导入时间
从selenium.webdriver导入Chrome
从selenium.webdriver.common.by导入By
从selenium.webdriver.support.wait导入WebDriverWait
导入selenium.webdriver.support.expected_conditions as EC
从selenium.common.exceptions导入NoSuchElementException

driver = Chrome()
driver.get(url="https://infoguia.com/bnom.asp?txt=clinicas&est=100")

clinics = driver.find_elements(By.CSS_SELECTOR, "div.perfil-sec-10-bottom-b-part-2")
links = [link.find_element(By.TAG_NAME, 'a').get_attribute('href') for link in clinics]

对于链接中的每个链接:
    driver.get(link)
    您的元素 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.full-breadcrumb')))
    尝试:
        driver.find_element(By.CSS_SELECTOR, 'button[id="view-whatsapp"]').click()
        num = driver.find_element(By.CSS_SELECTOR, 'a.fs-text-2.whatsapp-click-stats').get_attribute('innerHTML')
        打印(num)
    除了NoSuchElementException:
        对于那些没有WhatsApp号码的链接:
            跳过

    等待1秒

输出:

 (0412)264.2658
 (0426)371.9876
 (0424)154.3403
 (0424)127.5955
 (0412)687.3078
 (0424)577.7972
 (0412)601.6110
 (0412)717.8044
 (0414)819.8567
英文:

You can try this way,

import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

driver = Chrome()
driver.get(url="https://infoguia.com/bnom.asp?txt=clinicas&est=100")

clinics = driver.find_elements(By.CSS_SELECTOR, "div.perfil-sec-10-bottom-b-part-2")
links = [link.find_element(By.TAG_NAME, 'a').get_attribute('href') for link in clinics]

for link in links:
    driver.get(link)
    your_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.full-breadcrumb')))
    try:
        driver.find_element(By.CSS_SELECTOR, 'button[id="view-whatsapp"]').click()
        num = driver.find_element(By.CSS_SELECTOR, 'a.fs-text-2.whatsapp-click-stats').get_attribute('innerHTML')
        print(num)
    except NoSuchElementException:
        """for those links which don't have WhatsApp number"""
        pass

    time.sleep(1)

The idea is that you go to the mail URL and grab the links of all the clinics (here, it's 20). And then you can directly iterate over them to get the WhatsApp number, if any.

output:

 (0412)264.2658
 (0426)371.9876
 (0424)154.3403
 (0424)127.5955
 (0412)687.3078
 (0424)577.7972
 (0412)601.6110
 (0412)717.8044
 (0414)819.8567

huangapple
  • 本文由 发表于 2023年4月17日 07:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定