XPATH: .select_by_visible_text not working



这似乎是一个非常微不足道的问题,但由于某种原因,我的.select_by_visible_text不能在我的表单上工作。下面是XML布局:

<label for="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2945768361,4093122127418558670,multipleChoice)" class="fb-form-element-label" data-test-form-element-label="">

<span class="t-14 fb-form-element-label__title--is-required" data-test-form-element-label-title="true">
At the time of applying, are you 18 years of age or older?
</span>
<span class="visually-hidden" data-test-form-element-required="true">
Required
</span>
<!---->
<!---->
</label>
<div class="fb-dropdown" data-test-dropdown="">
<select id="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2945768361,4093122127418558670,multipleChoice)" class="  fb-dropdown__select" aria-describedby="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2945768361,4093122127418558670,multipleChoice)-error-message" data-test-fb-dropdown-select="">
<option value="">Select an option</option>
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</div>

我的代码成功地定位了所有的xpath,我不认为这是xpath的问题。但是,这里是我的代码供参考:

form_choice = driver.find_elements_by_xpath('//label[starts-with(@for, "urn:li:fs_easyApplyFormElement")][contains(@for, "multipleChoice")]')
for link in form_choice:
fill = False
try:
link.find_element_by_xpath('.//span[contains(., "18") or contains(., "applying") or contains(., "job")]')
link.find_element_by_xpath('.//following-sibling::div[1]//select').select_by_visible_text("Yes")
fill = True
form = True
except:
pass

请记住,我的xpath是这样格式化的,因为元素名从来都不是常量。反正它能找到正确的元素,所以这应该不是问题。奇怪的是,如果我输入link.find_element_by_xpath('.//following-sibling::div[1]//select').click(),它会点击正确的表单元素。然而,没有多少修订版能够让程序选择正确的下拉选项,包括在。select_by_visible_text命令之前或之后使用。click()。

提前感谢您的帮助!

(编辑)玩一些之后,我发现,我无法以任何方式从下拉菜单中选择任何东西,不只是select_by_visible_text。表单元素被清楚地定位和选中。我也试过waiting_until_clickable。它都是开放的,可以编写代码了。唯一的问题是让它选择一个选项。

select_by_visible_text是Select class instance的一个方法。试着

from selenium.webdriver.support.ui import Select
...
drop_down = Select(link.find_element_by_xpath('.//following-sibling::div[1]//select'))
drop_down.select_by_visible_text("Yes")

最新更新