Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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
错误消息:无法定位元素。有什么问题吗?Python 3.7、Selenium 3.141.0_Python_Python 3.x_Selenium_Selenium Webdriver_Webautomation - Fatal编程技术网

错误消息:无法定位元素。有什么问题吗?Python 3.7、Selenium 3.141.0

错误消息:无法定位元素。有什么问题吗?Python 3.7、Selenium 3.141.0,python,python-3.x,selenium,selenium-webdriver,webautomation,Python,Python 3.x,Selenium,Selenium Webdriver,Webautomation,当使用selenium使用Xpath查找元素标记时,它使用从chrome复制的Xpath为该元素工作,这是为了测试它是否工作。由于id元素在每次打开网页时都会更改,因此我知道这将导致我的web自动化程序出错,现在从同一个标记开始,我使用class元素,因为它不会在每次打开网页时更改,但Selenium根本找不到该元素。当我将xpath中的元素更改为class时,其相对名称来自最初从webbroswer复制和测试的xpath。有什么问题吗 下面是我想使用的html源代码和Xpath的示例: <

当使用selenium使用Xpath查找元素标记时,它使用从chrome复制的Xpath为该元素工作,这是为了测试它是否工作。由于id元素在每次打开网页时都会更改,因此我知道这将导致我的web自动化程序出错,现在从同一个标记开始,我使用class元素,因为它不会在每次打开网页时更改,但Selenium根本找不到该元素。当我将xpath中的元素更改为class时,其相对名称来自最初从webbroswer复制和测试的xpath。有什么问题吗

下面是我想使用的html源代码和Xpath的示例:

<div class="popover fade right in" role="tooltip" id="popover784483" style="top: -9px; left: 328.5px; display: block;">
    <div class="arrow" style="top: 88.9423%;"></div>
    <h3 class="popover-title">
        <font style="vertical-align: inherit;">
            <font style="vertical-align: inherit;">Member details</font>
        </font>
    </h3>
    <div class="popover-content">
        <img class="center-block" src="http://api.vdarts.net:8080/picture/portrait/default.png" style="max-width:200px;height:auto;">
        <hr>
        <font style="vertical-align: inherit;">
            <font style="vertical-align: inherit;">Account: CapitainJack </font>
要使用的Xpath:

//*[@class="popover fade right in"]/div[2]/font[1]/font
我建议您改用选择器

当一个元素有多个由空格分隔的类时,复合类名是有效的。例如,

如果要选择包含所有3个类的元素(按任意顺序),可以使用:

element = driver.find_element_by_css_selector(".class1.class2.class3")

如果您想使用XPath,我发现以下方法有效(使用与上面相同的示例):


注意:您可以使用不同的布尔运算符过滤掉某些类名(例如“and”和“not”等)

解决此问题的方法有很多

您可以像Xpath一样匹配部分文本内容

//*[contains(text(), 'Account')]
您可以使用Img标记和以下字体,如

(//img//following::font)[2]

发布第二个XPath的错误消息。尝试CSS选择器,
div[id^='popover']>div.popover-content>font>font
。对于XPath的修改版本,
//div[以(@id,'popover')开头]/div[2]/font[1]/font
,您可以做基本相同的事情。使用索引通常是一个坏主意,因为HTML可能会更改,并且您的定位器将不适应,例如,它将始终使用第二个div等。请更新您的问题标题<代码>Python 3.7,Selenium 3.141.0不是问题。
在Selenium中不能将复合类名与XPath一起使用。
这不是事实。虽然这不是一个好主意,但它可以用于XPath。我想你可能把
find\u element\u与\u class\u name()
混淆了,因为它只接受一个类名,不允许使用复合类名。。。。但我同意使用CSS选择器和您给出的示例。我已经尝试了您的两个选项,第二个选项可以工作,但无法准确检索到我想要的内容。我为您的第一个选项收到的错误消息是:消息:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“/*[@id=contains(text(),'popover')]/div[2]/font[1]/font”}尝试更改xpath中的顺序并使用Chrome开发工具进行检查。一个很好的方法是可用的。好的,我尝试了一些不同的东西,当我在浏览器中搜索xpath时,xpath可以工作。这是新的错误消息。可能是什么问题?消息:选择器无效:xpath表达式“/*/div[以(@id,“popover”)开头]/div[2]/text()[1]”的结果是:[对象文本]。它应该是一个元素。最有可能的问题是引号。尝试使用“/*/div[以(@id,'popover')开头]/div[2]/text()[1]”
//*[contains(text(), 'Account')]
(//img//following::font)[2]