【问题标题】:Python selenium select option from angular js drop downPython selenium 从 Angular js 下拉列表中选择选项
【发布时间】:2015-11-04 10:52:42
【问题描述】:

请打开这个网站-https://mobikwik.com/
有一个 Mobile 表单,其中第二项为 Select Operator。
我想使用 selenium webdriver 从此下拉列表中选择 - “创意”。
请帮忙。

另外,在选择想法后,我会得到一个新的下拉列表用于选择圆圈。需要选择孟买。

我的尝试:

driver.find_element_by_css_selector("li > span.ng-binding").click()
driver.find_element_by_xpath("//label[3]/i").click()
driver.find_element_by_css_selector("font > label > i").click()
driver.find_element_by_xpath("//section[@id='mainunit']/div/div[2]/div/div[2]/div/div/div/form/div[4]/p/dl/dd/ul/li[9]/span").click()
driver.find_element_by_xpath("//font/label[2]/i").click()

【问题讨论】:

  • 试试这个driver.find_element_by_xpath("//li[@class='ng-scope'][9]").click()
  • 要完成@JasonEstibeiro 的评论,首先尝试:driver.find_element_by_xpath("//span[@class='ng-binding']").click() 使选项可见,然后单击选项:driver.find_element_by_xpath("//li[@class='ng-scope'][9]").click()
  • 或者这似乎是一个更好的选择。试试看driver.find_element_by_xpath("//span[@class='ng-binding' and text()='Idea'][1]").click()。让我知道它们是否有效。
  • 两个选项都抛出相同的错误:selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with 看起来你必须先展开选项,然后再选择它。
  • @Javitronxo - 现在意识到了。我实际上已经手动点击了它(ng-binding 跨度)。无论如何,赞成你的答案。

标签: python selenium selenium-webdriver


【解决方案1】:

我在同一个网页上尝试了这段代码并成功了:

driver.find_element_by_xpath("//span[contains(text(), 'Select Operator')]").click()
driver.find_element_by_xpath("//span[contains(text(), 'Idea')]/..").click()

首先必须让选项面板可见,否则会抛出以下异常:

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

更新:

要选择孟买选项:

driver.find_element_by_xpath("//span[contains(text(), 'Select Circle')]").click()
driver.find_element_by_xpath("//span[contains(text(), 'Mumbai')]/..").click()

【讨论】:

  • 谢谢....它成功了...我已经更新了我的问题....n 尝试了很多方法来通过参考您提供的代码来实现相同..."在选择想法后,我为选择圈子获得一个新的下拉菜单。需要为其选择孟买“....你能帮忙吗
【解决方案2】:

首先点击选择框然后选择选项,选择你需要点击第三个孩子的沃达丰:

driver.find_element_by_css_selector(".select").click()
driver.find_element_by_css_selector(".options > ul:nth-child(1) > li:nth-child(3)").click()

其他选项如下:

.options > ul:nth-child(1) > li:nth-child(2) => Artiel
.options > ul:nth-child(1) > li:nth-child(3) => Vodafone
.options > ul:nth-child(1) > li:nth-child(4) => BSNL

选择第一个选择框后有2个选择框,你可以同样选择它,但通过find_elements_by_css_selector()和pulural

# select first one
driver.find_element_by_css_selector(".select").click()
driver.find_element_by_css_selector(".options > ul:nth-child(1) > li:nth-child(3)").click()
# select second selectbox
# you may need to sleep until second selectbox is available
sleep(1)
driver.find_elements_by_css_selector(".select")[1].click()
driver.find_element_by_css_selector(".options.open > ul:nth-child(1) > li:nth-child(5)").click() # 5 option is Mumbai

【讨论】:

  • 谢谢....它起作用了...我已经更新了我的问题....n 尝试了很多方法来通过参考您提供的代码来实现相同..."在选择想法后,我为选择圈子获得一个新的下拉菜单。需要为其选择孟买“....你能帮忙吗
【解决方案3】:

floorselectionWait = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'//*[@id="select_22"]'))) floorselection=driver.find_element(By.XPATH,'//*[@id="select_22"]') floorselection.click() flooroptionsWait = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//DIV[@class='md-text ng-binding'][text()='Level 7']"))) optionSelect=driver.find_element(By.XPATH,"//DIV[@class='md-text ng-binding'][text()='Level 7']") optionSelect.click() angularjs md-select md-option 在 selenium python 中选择

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 2021-05-24
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多