简体   繁体   English

获取元素值并使用 Python 和 Selenium 进行比较

[英]Get element value and compare using Python and Selenium

hi this is my first time with Python and Selenium so please be patient.嗨,这是我第一次使用 Python 和 Selenium,所以请耐心等待。

I have:我有:

<span id="map_x" class="gr een">36</span>
<span id="map_y" class="green">3</span>

So now I'm trying to comapare this value like that所以现在我正试图比较这个值

mapx = window.find_element_by_xpath("/html/body/div[6]/div[57]/div[1]/h1/span[2]")
mapy = window.find_element_by_xpath("/html/body/div[6]/div[57]/div[1]/h1/span[3]")
while int(mapx.get_attribute('value')) != 1 and int(mapy.get_attribute('value')) <= 40:

I want to do a loop while mapx is not 1 and mapy less than 40 but when I'm trying it:我想在 mapx 不是 1 且 mapy 小于 40 时做一个循环,但是当我尝试它时:

Traceback (most recent call last):
  File "C:\Users\", line 50, in <module>
    while int(mapx.get_attribute('value')) != 1 and int(mapy.get_attribute('value')) <= 40:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

I will be grateful for any help i was trying many other ways, but it still doesn't working.我会很感激我尝试了许多其他方法的任何帮助,但它仍然不起作用。

Its a valid error.这是一个有效的错误。 The span tag has no attribute value to extract from get_attribute() . span标记没有可从get_attribute()中提取的属性value It has only id and class attributes.它只有idclass属性。 And if it does not find, it will return None .如果没有找到,它将返回None

You need extract the text from those span tags and compare.您需要从这些span标签中提取text并进行比较。 Try like below and confirm:尝试如下并确认:

1: You can use .text to extract text from a tag . 1:您可以使用.texttag中提取text

while int(mapx.text) != 1 and int(mapy.text) <= 40:

2: You can use get_attribute(innerText) too. 2:你也可以使用get_attribute(innerText)

mapx = window.find_element_by_xpath("/html/body/div[6]/div[57]/div[1]/h1/span[2]")
mapy = window.find_element_by_xpath("/html/body/div[6]/div[57]/div[1]/h1/span[3]")
while int(mapx.get_attribute('innerText')) != 1 and int(mapy.get_attribute('innerText')) <= 40:

Instead of Absolute xpath opt for Relative xpaths.而不是Absolute xpath 选择Relative xpaths。 If Elements id are unique you can try like below:如果Elements id是唯一的,你可以像下面这样尝试:

mapx = window.find_element_by_id("map_x")
mapy = window.find_element_by_id("map_y")
# OR

mapx = window.find_element_by_xpath("//span[@id='map_x' and @class='gr een']")
mapy = window.find_element_by_xpath("//span[@id='map_y' and @class='green']")

You should use .text if this does not work we can work with get_attribute如果这不起作用,您应该使用.text我们可以使用get_attribute

do this:做这个:

mapx = window.find_element_by_xpath("/html/body/div[6]/div[57]/div[1]/h1/span[2]")
mapy = window.find_element_by_xpath("/html/body/div[6]/div[57]/div[1]/h1/span[3]")
while int(mapx.text) != 1 and int(mapy.text) <= 40:
    do while loop logic here

Also not sure if we have unique id map_x and map_y in HTMLDOM , if we have for也不确定我们是否在HTMLDOM中有唯一的 id map_xmap_y ,如果我们有

<span id="map_x" class="gr een">36</span>
<span id="map_y" class="green">3</span>

if it happens to be unique in nature, please use如果它恰好在自然界中是独一无二的,请使用

window.find_element_by_id('map_x')

similarly for other elements as well.其他元素也类似。

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

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