selenium 如何将坐标列表拆分为单独的变量?

b4wnujal  于 2023-02-04  发布在  其他
关注(0)|答案(3)|浏览(136)

我试着把找到的元素的坐标分解成这样

elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))

当我只需要找到只出现过一次的元素的坐标时,我就使用. location,但是我的问题是当我必须找到出现过不止一次的元素时,我试着这样做,但是不起作用

elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))

elementlist = []

for element in elements:
    elementlocation = element.location
    elementlist.append(location)
    print(elementlist)

x,y = elementlist.split(",")
print(x,y)

我试着得到多次出现的元素的坐标列表,将它们拆分成单独的变量x,y并打印出来
更新:

<div class="gl-MarketGroup_Wrapper ">
  <div class="srb-Market25Wrapping gl-Market_General gl-Market_General-columnheader gl-Market_General-haslabels gl-Market_General-pwidth12-5 ">
    <div class="srb-ParticipantLabelCentered gl-Market_General-cn1 ">
      <div class="srb-ParticipantLabelCentered_Name ">100</div>
    </div>
    <div class="srb-ParticipantLabelCentered gl-Market_General-cn1 ">
      <div class="srb-ParticipantLabelCentered_Name ">110</div>
    </div>
    <div class="srb-ParticipantLabelCentered gl-Market_General-cn1 ">
      <div class="srb-ParticipantLabelCentered_Name ">120</div>
    </div>
    <div class="srb-ParticipantLabelCentered gl-Market_General-cn1 ">
      <div class="srb-ParticipantLabelCentered_Name ">130</div>
    </div>
  </div>
  <div class="srb-Market375Wrapping gl-Market_General gl-Market_General-columnheader gl-Market_General-pwidth18-75 ">
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">1</span>
    </div>
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">2</span>
    </div>
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">3</span>
    </div>
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">1</span>
    </div>
  </div>
  <div class="srb-Market375Wrapping gl-Market_General gl-Market_General-columnheader gl-Market_General-pwidth18-75 ">
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">2</span>
    </div>
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">1</span>
    </div>
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">1</span>
    </div>
    <div class="gl-ParticipantOddsOnly gl-Participant_General gl-Market_General-cn1 ">
      <span class="gl-ParticipantOddsOnly_Odds">2</span>
    </div>
  </div>
</div>

我想查找包含文本"1"且与包含文本"110"的元素高度相同的元素
如图

所示

dw1jzc5e

dw1jzc5e1#

你没有说你的元素是什么,但是考虑到它们是某个类的示例,这个类有location属性:

element_locations = [x,y for x,y in element.location.split(",") for element in elements]
# This is gonna be a list [(x1,y1), (x2,y2), ...,(xn,yn)]
p5fdfcr1

p5fdfcr12#

位置*

**location**属性包含WebElement在可呈现画布中的位置,格式为 dict

溶液
要打印元素的 x,y 位置,可以用途:

print([element.location for element in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))])
# outputs -> [{'x': 100, 'y': 367}, {'x': 100, 'y': 684}, {'x': 100, 'y': 684}, {'x': 100, 'y': 684}, {'x': 100, 'y': 1917}]

要仅打印坐标:

print([(element.location['x'],element.location['y']) for element in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))])
# outputs -> [(100, 367), (100, 684), (100, 684), (100, 684), (100, 1917)]

您可以在Selenium: get coordinates or dimensions of element with Python中找到相关的详细讨论

8gsdolmq

8gsdolmq3#

正如我们所讨论的,有一种比使用位置和比较Y值更好的方法来解决这个问题。使用XPath可以从第一列获得所需的元素,然后获得同一行中的其他元素。这个HTML更难,因为它不是HTML TABLE,它看起来只像一个表,而是由DIV组成的网格。无论如何...
1.从第1列获取所需元素
我们可以简单地使用XPath

col1_value = "120"
//div[@class='srb-ParticipantLabelCentered_Name '][text()='" + col1_value + "']

1.获取包含所需元素的行号
一旦我们从步骤1中得到了想要的元素,我们需要得到它的行号。

count(//div[./div[@class='srb-ParticipantLabelCentered_Name '][text()='" + col1_value + "']]/preceding-sibling::div)

基本上,我们从步骤1中获取元素,在DOM中向上一级,然后计算前面的DIV。
1.从第2列获取同一行中的元素
在这里,我们只使用上一步中的row #作为第2列的索引,并使用预期文本colX_value从该列返回所需的元素

colX_value = "1"
//div[" + row + "]/span[@class='gl-ParticipantOddsOnly_Odds'][text()='" + colX_value + "']

把这些放在实际的代码中

col1_value = "120"
colX_value = "1"
row = driver.execute_script("document.evaluate(\"count(//div[./div[@class='srb-ParticipantLabelCentered_Name '][text()='" + col1_value + "']]/preceding-sibling::div)\", document, null, XPathResult.NUMBER_TYPE, null).numberValue") + 1
col2_value = driver.find_element(By.XPATH, "//div[" + row + "]/span[@class='gl-ParticipantOddsOnly_Odds'][text()='" + colX_value + "']").text

相关问题