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

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

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

问题

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

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

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

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

  1. url = "https://infoguia.com/bnom.asp?txt=clinicas&est=100"
  2. # 创建一个新的Chrome会话
  3. driver = webdriver.Chrome(r'C:\Users\hamxa\Desktop\Chromdriver\chromedriver.exe')
  4. driver.implicitly_wait(50)
  5. driver.get(url)
  6. # 获取页面上诊所的列表
  7. clinics = driver.find_elements_by_class_name("grey-btn-profile.fs-text-button-1.fw-bold")
  8. # 循环遍历每个诊所以获取其电话号码
  9. for clinic in clinics:
  10. clinic.click() #打开每个诊所的详细页面
  11. try:
  12. whatsapp_button = driver.find_element_by_id('view-whatsapp')
  13. whatsapp_button.click()
  14. # 使用类名“fs-text-2 whatsapp-click-stats”查找号码
  15. number = driver.find_element_by_class_name('fs-text-2 whatsapp-click-stats')
  16. # 打印号码
  17. print(number.text)
  18. finally:
  19. driver.back() # 返回到搜索结果页面
  20. # 关闭webdriver
  21. 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

  1. url = "https://infoguia.com/bnom.asp?txt=clinicas&est=100"
  2. # create a new Chrome session
  3. driver = webdriver.Chrome(r'C:\Users\hamxa\Desktop\Chromdriver\chromedriver.exe')
  4. driver.implicitly_wait(50)
  5. driver.get(url)
  6. # get list of clinics on the page
  7. clinics = driver.find_elements_by_class_name("grey-btn-profile.fs-text-button-1.fw-bold")
  8. # loop through each clinic to get its phone number
  9. for clinic in clinics:
  10. clinic.click() #open detail page for each clinic
  11. try:
  12. # element = WebDriverWait(driver, 10).until(
  13. # EC.presence_of_element_located((By.CLASS_NAME, "fs-text-2.phone-click-stats"))
  14. # )
  15. whatsapp_button = driver.find_element_by_id('view-whatsapp')
  16. whatsapp_button.click()
  17. # find the number with the class "fs-text-2 whatsapp-click-stats"
  18. number = driver.find_element_by_class_name('fs-text-2 whatsapp-click-stats')
  19. # print the number
  20. print(number.text)
  21. finally:
  22. driver.back() # go back to the search results page
  23. # close the webdriver
  24. driver.close()

and this is the error i'm getting:

  1. 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

你可以尝试这样做,

  1. 导入时间
  2. selenium.webdriver导入Chrome
  3. selenium.webdriver.common.by导入By
  4. selenium.webdriver.support.wait导入WebDriverWait
  5. 导入selenium.webdriver.support.expected_conditions as EC
  6. selenium.common.exceptions导入NoSuchElementException
  7. driver = Chrome()
  8. driver.get(url="https://infoguia.com/bnom.asp?txt=clinicas&est=100")
  9. clinics = driver.find_elements(By.CSS_SELECTOR, "div.perfil-sec-10-bottom-b-part-2")
  10. links = [link.find_element(By.TAG_NAME, 'a').get_attribute('href') for link in clinics]
  11. 对于链接中的每个链接:
  12. driver.get(link)
  13. 您的元素 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.full-breadcrumb')))
  14. 尝试:
  15. driver.find_element(By.CSS_SELECTOR, 'button[id="view-whatsapp"]').click()
  16. num = driver.find_element(By.CSS_SELECTOR, 'a.fs-text-2.whatsapp-click-stats').get_attribute('innerHTML')
  17. 打印(num)
  18. 除了NoSuchElementException
  19. 对于那些没有WhatsApp号码的链接:
  20. 跳过
  21. 等待1

输出:

  1. (0412)264.2658
  2. (0426)371.9876
  3. (0424)154.3403
  4. (0424)127.5955
  5. (0412)687.3078
  6. (0424)577.7972
  7. (0412)601.6110
  8. (0412)717.8044
  9. (0414)819.8567
英文:

You can try this way,

  1. import time
  2. from selenium.webdriver import Chrome
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.wait import WebDriverWait
  5. import selenium.webdriver.support.expected_conditions as EC
  6. from selenium.common.exceptions import NoSuchElementException
  7. driver = Chrome()
  8. driver.get(url="https://infoguia.com/bnom.asp?txt=clinicas&est=100")
  9. clinics = driver.find_elements(By.CSS_SELECTOR, "div.perfil-sec-10-bottom-b-part-2")
  10. links = [link.find_element(By.TAG_NAME, 'a').get_attribute('href') for link in clinics]
  11. for link in links:
  12. driver.get(link)
  13. your_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.full-breadcrumb')))
  14. try:
  15. driver.find_element(By.CSS_SELECTOR, 'button[id="view-whatsapp"]').click()
  16. num = driver.find_element(By.CSS_SELECTOR, 'a.fs-text-2.whatsapp-click-stats').get_attribute('innerHTML')
  17. print(num)
  18. except NoSuchElementException:
  19. """for those links which don't have WhatsApp number"""
  20. pass
  21. 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:

  1. (0412)264.2658
  2. (0426)371.9876
  3. (0424)154.3403
  4. (0424)127.5955
  5. (0412)687.3078
  6. (0424)577.7972
  7. (0412)601.6110
  8. (0412)717.8044
  9. (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:

确定