简体   繁体   English

如何在硒python中查找元素

[英]How to find element in selenium python

I have been using selenium for a little bit now and have been stuck on this issue for the past few hours. 我现在已经使用硒了一段时间,并且在过去的几个小时中一直被困在这个问题上。 It seems really simple but I just can't seem to figure out a proper execution. 看起来确实很简单,但我似乎无法弄清楚正确的执行方法。 I basically am trying to select a shoe size based on the user's input. 我基本上是想根据用户的输入来选择鞋子的尺码。 Here are a few of the buttons I am trying to sort through as well as the surrounding HTML. 这是我尝试排序的一些按钮以及周围的HTML。

<p class="checkbox-size">
    <input type="radio" value="500" id="super_attribute[150]_500" name="super_attribute[150]" class="product_attribute">
    <label for="super_attribute[150]_500">
        <span id="label_eu0" class="label_hidden"> 38.5</span>
        <span id="label_us0" class="label_show"> 6</span>
        <span id="label_uk0" class="label_hidden"> 5.5</span>
        <span id="label_cm0" class="label_hidden"> 24</span>
    </label>
</p>

The specific button I am trying to press is this one: 我要按的特定按钮是此按钮:

<label for="super_attribute[150]_137">
    <span id="label_eu10" class="label_hidden"> 45</span>
    <span id="label_us10" class="label_show"> 11</span>
    <span id="label_uk10" class="label_hidden"> 10</span>
    <span id="label_cm10" class="label_hidden"> 29</span>
</label>

Now I tried many different methods of searching then clicking one of the buttons but nothing has worked yet. 现在,我尝试了许多不同的搜索方法,然后单击其中一个按钮,但是什么都没起作用。 Any suggestions? 有什么建议么? Here is what I am currently using to try and find and click the button: 这是我目前用来尝试查找并单击按钮的内容:

driver.find_element_by_css_selector("input[type='radio'][value='11']").click()

Looking back I might have not provided the correct code for the buttons, so here is a snapshot of the inspect element as well as the actual page if you want to go check it out for yourself. 回想一下,我可能没有为按钮提供正确的代码,因此,如果您想亲自检查一下,这是inspect元素以及实际页面的快照。 I am trying to click the size buttons. 我试图单击尺寸按钮。 Button inspect element 按钮检查元素

Solved! 解决了! Here is the code I ended up using. 这是我最终使用的代码。

sizes = driver.find_elements_by_class_name('checkbox-size')
for size in sizes:
    if size.text in [usersize]:
        size.click()
        print colored('Carted size %s'%(size.text), 'green')
        break
    continue

There are a few problems with your approach. 您的方法存在一些问题。

  1. You are trying to click the INPUT when you need to click a SPAN 当您需要单击SPAN时,您尝试单击INPUT

  2. The sizes on the page contain some amount of whitespace which makes your strict string comparison, value='11', not work. 页面上的大小包含一定数量的空格,这会使您进行严格的字符串比较,value = '11',不起作用。 Also the contained text is not considered a value . 同样,所包含的文本也不视为value

  3. Another issue you will run into is that all of the sizes aren't displayed. 您将遇到的另一个问题是未显示所有尺寸。 The sizes displayed is controlled by the links above the sizes, EU, US, UK, and CM. 显示的尺寸由尺寸,欧盟,美国,英国和CM上方的链接控制。 The ones that are displayed have class="label_show" so you will want to specify that in your answer or you will attempt to click elements that are not visible which will throw an exception. 显示的元素具有class="label_show"因此您需要在答案中指定该元素,否则将尝试单击不可见的元素,这将引发异常。 (Selenium only interacts with visible elements by design). (硒仅通过设计与可见元素相互作用)。

With all this, we can build the following XPath 有了这些,我们可以构建以下XPath

//span[@class='label_show'][normalize-space(.)='10']

If it were me, I would throw this in a function that pass in the desired size as a string and insert that parameter into the above XPath to make this much more reusable. 如果是我,我会将其扔入一个函数中,该函数以所需的大小作为字符串传递,并将该参数插入上述XPath中,以使其更加可重用。

试试下面的定位器(xpath):

.//span[contains(@id,'label_eu') and text()=' 40']

Ok so here is what I ended up using which solved the issue. 好的,这就是我最终使用的解决问题的方法。 I made a list of all the web elements with the class 'checkbox-size' then traversed that list to to find the correct size, and then click on that element. 我用'checkbox-size'类列出了所有Web元素的列表,然后遍历该列表以找到正确的大小,然后单击该元素。 Here is the code: 这是代码:

    sizes = driver.find_elements_by_class_name('checkbox-size')
for size in sizes:
    if size.text in [usersize]:
        size.click()
        print colored('Carted size %s'%(size.text), 'green')
        break
    continue

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM