简体   繁体   English

如何使用Selenium / python根据Excel工作表中给出的值从下拉列表中选择一个值

[英]How to select a value from drop down based on the value given from Excel sheet using selenium/python

I am currently learning to automate a web application which contains drop down.The thing is am able to select the the value from drop down by giving it directly in the xpath.Now i am trying to select a value from drop down based on the value given in the excel sheet rather than giving it directly.I dont know how to go about it . 我目前正在学习自动化包含下拉列表的Web应用程序。事情是能够通过直接在xpath中提供它来从下拉列表中选择值。现在我正在尝试根据该值从下拉列表中选择一个值在Excel工作表中给出而不是直接给出。我不知道该怎么做。

Please find the sample HTML 请找到示例HTML

<select class="parent" id="parent_name" name="parentId">                                      
  <option value="1">A</option>
  <option value="2">B</option>
  <option value="3">C</option>
  </select>

Please find the code i tried for drop down 请找到我尝试下拉的代码

dropdown=driver.find_element_by_xpath('//select[@class="parent"]//option[2]');
dropdown.click();

For selecting the value from excel for others thing like text box i am using the below code 为了从Excel中为文本框等其他东西选择值,我正在使用以下代码

driver = webdriver.Chrome  
driver.fullscreen_window();
driver.get('url');
time.sleep(5) 
Workbook=xlrd.open_workbook("excel path")
Details = Workbook.sheet_by_index(0);
city=Details .cell(1,0)
Citytextbox=driver.find_element_by_xpath('//input[@id="city"]');
Citytextbox.send_keys(city.value);

What i am expecting is if i give value as "A" in excel,it should be able to select it from drop down rather than hard coding it am using spyder-python 3.7. 我期望的是,如果我在excel中将值赋为“ A”,它应该能够从下拉列表中选择它,而不是使用spyder-python 3.7对其进行硬编码。

Update :Tried following two methods i facing the below issue .can you guide me on what the issue might be. 更新 :我尝试了以下两种方法来面对以下问题。您能指导我解决什么问题。 The first method ,it seems noting is happening,code is running even with selecting the drop down and no error is shows.It seems it didn't come into code 第一种方法,似乎正在发生,即使选择了下拉菜单,代码仍在运行,并且未显示任何错误。似乎没有纳入代码

 class test:
        def test(self):
           type=details.cell(1,13);
    dropdown=selected(self.driver.find_element_by_xpath('//select[@class="parent"]')); 
               dropdown.select_by_value(str(type));
               time.sleep(5);

Second method :It it shows -module' object is not callable.Added import select as selected 第二种方法 :它显示-module'对象是不可调用的。

type=details.cell(1,13);
dropdown=selected(driver.find_element_by_xpath('//select[@class="parent"]'));
dropdown.select_by_value(str(type));

When i try it by the above method i am getting this error TypeError: 'module' object is not callable 当我通过上述方法尝试时,出现此错误TypeError:“模块”对象不可调用

You will want to use the Select object . 您将要使用Select对象 First of all pull the relevant value out of your Excel document (you'll need to tweak the below to target the relevant cell, I've provided an example based on your question): 首先,从Excel文档中提取相关值(您需要调整以下内容以定位相关单元格,我根据您的问题提供了一个示例):

Workbook = xlrd.open_workbook("excel path")
Details = Workbook.sheet_by_index(0);
selectValue = Details .cell(1,0)

Then you will need to use the value as shown below: 然后,您将需要使用如下所示的值:

select = Select(self.driver.find_element_by_id("parent_name")) 
select.select_by_value(str(selectValue))

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

相关问题 无法使用python中的Selenium从下拉列表中选择一个值 - Cant select a value from a drop down list using Selenium in python 如何从 python selenium 的下拉菜单中获取 select 的值 - How to select a value from drop down menu in python selenium 如何从具有特殊设置的网站上使用Selenium从下拉列表中选择一个值-Python - How to select a value from a drop-down using Selenium from a website with special setting- Python 如何 select 使用 selenium python 来自谷歌表单下拉元素的值 - How to select a value from a google form drop down element using selenium python 如何使用 python 从 selenium webdriver 的下拉列表中选择一个值? - How do I select a value from drop down in selenium webdriver using python? 如何将select从下拉菜单中取值Selenium using Python - How to select from the drop-down menu value with Selenium using Python 无法从硒python的下拉列表中选择一个值 - Unable to select a value from a drop-down in selenium python 如何使用 Python 与 Selenium 一起 select 下拉菜单值? - How to select a drop-down menu value with Selenium using Python? 如何选择一个下拉菜单值与硒使用 python - how-to-select-a-drop-down-menu-value-with-selenium-using-python 使用selenium python从下拉选项中选择一个值 - Selecting a value from a drop-down option using selenium python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM