Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpliterPython脚本按钮单击_Python_Selenium_Splinter - Fatal编程技术网

SpliterPython脚本按钮单击

SpliterPython脚本按钮单击,python,selenium,splinter,Python,Selenium,Splinter,我试图写一个代码,让我选择我选择的性别(让我们以女性为例)。该元素如下所示。请注意,ID保持更改,因此我无法使用带有ID的css选择器 请建议在硒中使用哪种方法 谢谢 <div id="557f72b0-c612-428a-a77e-eb0841869ddb" class="nike-unite-gender-buttons gender nike-unite-component"> <div class="shim"></div> <d

我试图写一个代码,让我选择我选择的性别(让我们以女性为例)。该元素如下所示。请注意,ID保持更改,因此我无法使用带有ID的css选择器

请建议在硒中使用哪种方法

谢谢

<div id="557f72b0-c612-428a-a77e-eb0841869ddb" class="nike-unite-gender-buttons gender nike-unite-component">
    <div class="shim"></div>
    <div class="error"></div>

    <input type="hidden" id="40d664f0-e498-4fa2-8a6f-7d9fc75cfe01" value="" name="gender" data-componentname="gender">

    <ul data-componentname="gender">

    <li id="222f5bc2-171e-450e-8182-8e15c8d9f47b" class="">
    <input type="button">
    <span>Male</span>
    </li>

    <li id="6c6098bb-fc9f-497e-8551-0ae6bb8a235c" class="">
    <input type="button">
    <span>Female</span>
    <</li>

    </ul>
    <div class="tip">Please select a gender.</div>
</div>     

  • 男性
  • 女性
    首先,通过类名找到性别容器:

    gender_container = browser.find_by_css('.nike-unite-gender-buttons.gender').first
    
    然后,通过
    span
    元素中的以下文本找到按钮:

    female = gender_container.find_by_xpath('.//li[span = "Female"]/input').first
    female.click()
    

    或者,您可以尝试直接通过文本查找元素:

    browser.find_by_text('Female').first.click()
    

    您是否尝试使用
    XPath
    选择器,例如
    //span[text='Female']
    ?显示您的尝试和异常logs@Andersson你是说
    text()
    ?:)顺便说一下,在splinter中有一个方便的
    find_By_text()
    快捷方式。哦。。是的,我的意思是
    //span[text()='Female']
    。非常感谢你。它成功了,我尝试了所有的方法,但是用文本法找到了。哈哈…@xBillBillx,如果它解决了你的问题,你可能会