Selenium, 选择<span>内的一个元素[英] Selenium, Selecting an element inside of a <span>

问题描述

错误是:

Select only works on <select> elements, not on <span>.  Trying to accomplish this using PhantomJS & Selenium in Python.

最接近我可能会找到答案是:

如何选择此跨度元素?

试图学习硒自动化,并认为与Gmail一起工作将是一个挑战.现在试图在帐户创建阶段选择"出生月".

https://accounts.google.com/signup?service=mail&continue = https%3a%2f%2f%2fmail.google.com%2fmail%2f<mpl=defuath&hl = en

现在,尝试通过XPath选择,如下(以及上面导致错误的原因).

select_birthday_month = Select(driver.find_element_by_xpath('//span[@id="BirthMonth"]')

Gmail页面上的代码为:

<div class="form-element multi-field birthday" id="birthday-form-element">
  <fieldset>
  <legend><strong id="BirthdayLabel">Birthday</strong></legend>
  <label id="month-label" class="month">
  <span id="BirthMonthHolder" >
  <select id="BirthMonth" name="BirthMonth">
  <option value="">Month</option>
  <option value="01" >
  January</option>
  <option value="02" >
  February</option>
  <option value="03" >
  March</option>
  <option value="04" >
  April</option>
  <option value="05" >
  May</option>
  <option value="06" >
  June</option>
  <option value="07" >
  July</option>
  <option value="08" >
  August</option>
  <option value="09" >
  September</option>
  <option value="10" >
  October</option>
  <option value="11" >
  November</option>
  <option value="12" >
  December</option>
  </select>
  </span>
  </label>

我尝试过:

 select_birthday_month = driver.find_element_by_xpath('//span[@id="BirthMonth"].click() 

(正如其他一些Stackoverflow答案遇到此问题时所建议的).然后选择一个月值的X-PATH.但是我遇到了错误的错误,说明X-Path找不到.

谁能建议这样做的最佳方法?我需要先单击元素吗?有什么好方法可以得到"棘手"的X-Paths?右键单击/检查Chrome上的元素适用于基本内容,但是当进入下拉列表时 - 它不会剪切.

推荐答案

您遇到的问题是因为从Google中选择元素没有像普通选择选项那样实现.因此,从技术上讲,您应该避免使用选择.

但是,有一个解决方案,首先我们检查有关Google如何渲染HTML选择选项的该元素,它显示了类似的内容:

<span aria-invalid="false" class=" " id="BirthMonth">
<div aria-activedescendant=":0" title="Birthday" aria-haspopup="true" tabindex="0" aria-expanded="false" style="-moz-user-select: none;" role="listbox" class="goog-inline-block goog-flat-menu-button jfk-select">
<div aria-posinset="3" aria-setsize="12" role="option" id=":0" class="goog-inline-block goog-flat-menu-button-caption">March</div>
<div aria-hidden="true" class="goog-inline-block goog-flat-menu-button-dropdown">&nbsp;</div></div><div aria-haspopup="true" role="listbox" style="-moz-user-select: none; visibility: visible; left: 0px; top: -204px; display: none;" class="goog-menu goog-menu-vertical"><div id=":1" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">January</div></div><div id=":2" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">February</div></div><div id=":3" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">March</div></div><div id=":4" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">April</div></div><div id=":5" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">May</div></div><div id=":6" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">June</div></div><div id=":7" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">July</div></div><div id=":8" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">August</div></div><div id=":9" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">September</div></div><div id=":a" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">October</div></div><div id=":b" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">November</div></div><div id=":c" style="-moz-user-select: none;" role="option" class="goog-menuitem"><div class="goog-menuitem-content">December</div></div></div><input value="03" id="HiddenBirthMonth" name="BirthMonth" type="hidden"></span>
... and the rest ...

Google在 span 元素下已将正常选项置于不同的动态 divs ,您将无法选择 a div 或 span ,因此您必须思考替代方案.

幸运的是,动态 div 具有称为goog-inline-block goog-flat-menu-button jfk-select的类,您只需要找到此元素,然后将键发送到 select> select 的动作/strong> <- 我的意思是发送键入键.

好吧,这是工作解决方案:

...
# I find the div by class_name, of course you can use xpath
select_birthday_month = driver.find_element_by_class_name('jfk-select')
select_birthday_month.send_keys('January')
...

就是这样!有时您只需要在框外思考即可解决类似问题,希望这会有所帮助:)

请参阅结果:

在此处输入图像说明

本文地址:https://www.itbaoku.cn/post/1740011.html