Selenium / select dropbox?

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

Selenium / select dropbox?

问题

我尝试使用Selenium访问以下网站:https://gesund.bund.de/suchen/aerztinnen-und-aerzte,并使用以下代码:

import time
import os, sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager

if __name__ == '__main__':
  path = os.path.abspath(os.path.dirname(sys.argv[0]))

  options = Options()
  options.add_argument("start-maximized")
  options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
  options.add_experimental_option("excludeSwitches", ["enable-automation"])
  options.add_experimental_option('excludeSwitches', ['enable-logging'])
  options.add_experimental_option('useAutomationExtension', False)
  options.add_argument('--disable-blink-features=AutomationControlled')
  srv=Service(ChromeDriverManager().install())
  driver = webdriver.Chrome(service=srv, options=options)
  waitWD = WebDriverWait(driver, 10)

  link = f"https://gesund.bund.de/suchen/aerztinnen-und-aerzte"
  driver.get(link)
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="arztsuche__field_where"]'))).clear()
  time.sleep(0.5)
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="arztsuche__field_where"]'))).send_keys("13403")
  time.sleep(0.5)

  # select = Select(driver.find_element(By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))
  # driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]))))
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]')).send_keys("Hausarzt")

input("Press!")

我尝试选择"科室"下拉框中的一个选项,但无法成功。我尝试了几种方法,如您在注释中所见:

# select = Select(driver.find_element(By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))
# driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]))))
waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]')).send_keys("Hausarzt")

我尝试使用Select(但它告诉我它不是select元素)。尝试点击它 - 但它不可点击。尝试简单地分配文本给它 - 但这也不起作用。

如何自动化操作该网站上的元素?

英文:

i try to use selenium with this site:

https://gesund.bund.de/suchen/aerztinnen-und-aerzte

with the following code:

import time
import os, sys

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
# from fake_useragent import UserAgent

if __name__ == '__main__':
  path = os.path.abspath(os.path.dirname(sys.argv[0]))

  options = Options()
  options.add_argument("start-maximized")
  options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
  options.add_experimental_option("excludeSwitches", ["enable-automation"])
  options.add_experimental_option('excludeSwitches', ['enable-logging'])
  options.add_experimental_option('useAutomationExtension', False)
  options.add_argument('--disable-blink-features=AutomationControlled')
  srv=Service(ChromeDriverManager().install())
  driver = webdriver.Chrome (service=srv, options=options)
  waitWD = WebDriverWait (driver, 10)

  link = f"https://gesund.bund.de/suchen/aerztinnen-und-aerzte"  
  driver.get (link)
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="arztsuche__field_where"]'))).clear()
  time.sleep(0.5)
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="arztsuche__field_where"]'))).send_keys("13403")
  time.sleep(0.5)

  # select = Select(driver.find_element(By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))
  # driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]'))))    
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))).send_keys("Hausarzt")    

input("Press!")

I try to select an entry from the combobox "Fachrichtung" i the middle but it is not working. I tried several thing like you can see in the comment:

  # select = Select(driver.find_element(By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))
  # driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]'))))    
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))).send_keys("Hausarzt") 

Tried to use select (but it tells me its no select-element). Tried to click on it - but it is not clickable. And tried to simple assign a text to it - but this is also not working.

How can i automate the element on this site?

答案1

得分: 1

以下是您要翻译的代码部分:

It is an Auto Complete form, `Select` won't work here.
You need to identify the element and then click on the element after search result appear on the autocomplete form.

waitWD = WebDriverWait(driver, 10)

link = "https://gesund.bund.de/suchen/aerztinnen-und-aerzte"
driver.get(link)
searchstring = "13403"
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="arztsuche__field_where"]'))).clear()
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="arztsuche__field_where"]'))).send_keys(searchstring)
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='{}']".format(searchstring))).click()

Browser Snapshot:

[![enter image description here][1]][1]

UPDATE:

waitWD = WebDriverWait(driver, 10)

link = "https://gesund.bund.de/suchen/aerztinnen-und-aerzte"
driver.get(link)
searchstring = "13403"
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="arztsuche__field_where"]')).clear()
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//input[@id="arztsuche__field_where"]')).send_keys(searchstring)
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='{}']".format(searchstring))).click()

waitWD.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#arztsuche-fachrichtung"))).click()
secondField = 'Hausarzt'
waitWD.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='{}']".format(secondField))).click()

Browser Snapshot:

[![enter image description here][2]][2]

[1]: https://i.stack.imgur.com/udXJx.png
[2]: https://i.stack.imgur.com/GZWNI.png
英文:

It is an Auto Complete form,Select won't work here.
You need to identify the element and then click on the element after search result appear on the autocomplete form.

waitWD = WebDriverWait (driver, 10)

link = "https://gesund.bund.de/suchen/aerztinnen-und-aerzte"  
driver.get (link)
searchstring="13403"
waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).clear()
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).send_keys(searchstring)
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='{}']".format(searchstring)))).click()

Browser Snapshot:

Selenium / select dropbox?

UPDATE:

waitWD = WebDriverWait (driver, 10)

link = "https://gesund.bund.de/suchen/aerztinnen-und-aerzte"  
driver.get (link)
searchstring="13403"
waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).clear()
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).send_keys(searchstring)
time.sleep(0.5)
waitWD.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='{}']".format(searchstring)))).click()

waitWD.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#arztsuche-fachrichtung"))).click()
secondField='Hausarzt'
waitWD.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='{}']".format(secondField)))).click()

BrowserSnapshot

Selenium / select dropbox?

huangapple
  • 本文由 发表于 2023年2月24日 16:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554354.html
匿名

发表评论

匿名网友

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

确定