简体   繁体   English

在硒中找不到元素。 XPath,CSS选择器,我已经尝试了所有方法

[英]Can't locate element in selenium. XPath, css selectors, I've tried everything

 Impressions <div class="ellipsis _1ha3" data-hover="tooltip" data-tooltip-display="overflow" data-tooltip-text-direction="auto">11,483</div> Clicks <div class="ellipsis _1ha3" data-hover="tooltip" data-tooltip-display="overflow" data-tooltip-text-direction="auto">379</div> 

I'm currently stuck. 我目前陷入困境。 I'm running two pieces of identical code to pull metrics from fb ad manager, and although the top code works fine, the second code (the one trying to find clicks) can't seem to find the element i'm looking for. 我正在运行两段相同的代码以从fb广告管理器中提取指标,尽管最上面的代码可以正常工作,但是第二个代码(一个试图找到点击次数的代码)似乎找不到我想要的元素。 I've tried xpath, css_selectors, everything, and still can't seem to find out why I can't locate that element. 我已经尝试了xpath,css_selectors,所有方法,但似乎仍然找不到我找不到该元素的原因。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you! 谢谢!

Impressions 印象

impressions = browser.find_element_by_xpath('//*[@id="ads_pe_container"]/div[2]/div[2]/div[4]/div/div/div/div/div[2]/div/div/div[1]/div[1]/div/div[2]/div/div/div[1]/div[4]/div/div/div[2]/div/div[5]/div/div/div/div[1]')
impressions_text = impressions.text
impressions_attribute_value = impressions.get_attribute('value')
impressions = ('{0}'.format(impressions_text))
print(impressions)

Clicks 点击

clicks = browser.find_element_by_xpath('//*[@id="ads_pe_container"]/div[2]/div[2]/div[4]/div/div/div/div/div[2]/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[4]/div/div/div[2]/div/div[7]/div/div/div/div[1]')
clicks_text = clicks.text
clicks_attribute_value = clicks.get_attribute('value')
clicks = ('{0}'.format(clicks_text))
print(clicks)

I'm expecting for #click to return a number like #impressions did, but it can't seem to find the element. 我期望#click像#impressions一样返回一个数字,但似乎找不到该元素。 I've even tried switching frames to no avail. 我什至尝试切换帧都无济于事。 The #impressions is located in DOM window, so that wasn't an issue. #impressions位于DOM窗口中,所以这不是问题。 Any help would be appreciated. 任何帮助,将不胜感激。

a copy of the element #clicks 元素的副本#clicks

you shold always try and trim selectors, make them as thin as possible. 您应始终尝试修剪修剪器,使其尽可能薄。

as selector you should use: 作为选择器,您应该使用:

This selector will match exact className: 该选择器将匹配完全相同的className:

xpath('//div[@class="ellipsis _1ha3"][x]')

or 要么

This selector will match elements class where it contains this chars: 该选择器将匹配包含以下字符的元素类:

xpath('//div[contains(@class, "ellipsis _1ha3")][x]')
  • x is the position of retrieved dom elements, starts from 1, ([1] will select first occurence and so on). x是检索到的dom元素的位置,从1开始([1将选择第一个出现的位置,依此类推)。

The variable for occurrence must be placed after the complete xpath: 必须将出现的变量放在完整的xpath之后:

xpath('(//div[contains(@class, "ellipsis _1ha3")])[x]')

or 要么

xpath( ' (//div[@class='ellipsis _1ha3'])[x]')

You can try the xpath in console of Chrome , as mentioned below: 您可以在Chrome的控制台中尝试xpath,如下所述:

$x("(//div[contains(@class,'ellipsis _1ha3')])[x]")

If the class name has to be matched exactly then: 如果必须精确匹配类名,则:

$x("(//div[@class='ellipsis _1ha3'])[x]")

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

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