简体   繁体   English

如何在 python 中使用 selenium select 下拉列表中的一个项目

[英]how to select an item of a dropdown using selenium in python

I'm trying to click on "Universität Bern" which is an option of a dropdown that can be found in the following link :我试图点击"Universität Bern" ,这是一个下拉选项,可以在以下链接中找到:

I used the following code to get to the page (link), but I'm not able to click on an opetion of this dropdown.我使用以下代码进入页面(链接),但无法单击此下拉菜单的选项。 My code is:我的代码是:

     import content as content
     from selenium import webdriver
     from selenium.webdriver.support.ui import Select
     import time

     path = "C:\Program Files\chromedriver.exe"
     driver = webdriver.Chrome(path)
     driver.get("https://www.zssw.unibe.ch/usp/zms/angebot/6728/index_ger.html")
     pathanmelden = driver.find_element_by_xpath("//* 
     [@id='content']/section/div/div/div/div/div/table/tbody/tr[5]/td[2]/a")
     pathanmelden.click()
     time.sleep(1)
     pathforstudents = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/form/input")
     pathforstudents.click()
     chosetheuniversity = driver.find_element_by_class_name("")# This is what does not work"

I appreciate any help.我感谢任何帮助。

This drop down is not build using select and option tag so select class won't work.此下拉列表不是使用 select 和选项标签构建的,因此 select class 将不起作用。

You should first click on drop down arrow and then click on the desired element.您应该首先单击下拉箭头,然后单击所需的元素。

Code:代码:

driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://www.zssw.unibe.ch/usp/zms/angebot/6728/index_ger.html")
pathanmelden = driver.find_element_by_xpath("//* [@id='content']/section/div/div/div/div/div/table/tbody/tr[5]/td[2]/a")
pathanmelden.click()
time.sleep(1)
pathforstudents = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/form/input")
pathforstudents.click()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img#user_idp_iddicon"))).click()
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@title='Universities: Universität Bern']"))).click()

Imports:进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

PS: I have not changed your existing locators, you can change them as well since they are absolute xpath. time.sleep(2) is just for visualization purposes. PS:我没有更改您现有的定位器,您也可以更改它们,因为它们是绝对值 xpath。time.sleep time.sleep(2)仅用于可视化目的。 You can remove that once you test out the code.一旦你测试了代码,你就可以删除它。

Try Following by sending your text then press Enter:尝试通过发送您的文本进行关注,然后按 Enter 键:

Java Code: Java 代码:

WebDriver driver;
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30, 1));
driver.manage().window().maximize();

driver.get("https://www.zssw.unibe.ch/usp/zms/login.php?");
By y = By.xpath("//INPUT[@id='user_idp_iddtext']");
driver.findElement(y).clear();
Thread.sleep(2);
driver.findElement(y).sendKeys("Universität Bern");
Thread.sleep(2);
driver.findElement(y).sendKeys(Keys.ENTER);

Python: Python:

driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://www.zssw.unibe.ch/usp/zms/angebot/6728/index_ger.html")
x= driver.find_element_by_xpath("//INPUT[@id='user_idp_iddtext']")
x.clear()
time.sleep(1)
x.send_Keys("Universität Bern")
time.sleep(1)
x.send_Keys(Keys.ENTER)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM