Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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/selenium/4.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
在PythonSelenium中同时获取ID和类的xpath_Python_Selenium_Xpath - Fatal编程技术网

在PythonSelenium中同时获取ID和类的xpath

在PythonSelenium中同时获取ID和类的xpath,python,selenium,xpath,Python,Selenium,Xpath,在python selenium中,如何为只需要id和类的以下代码创建xpath: <button type="button" id="ext-gen756" class=" x-btn-text">Save</button> driver.find_element_by_xpath("//button[@id='ext-gen756'][@class=' x-btn-text']") 我不想提及像这样的序列号- //div[@class='x-combo-list-

在python selenium中,如何为只需要id和类的以下代码创建xpath:

<button type="button" id="ext-gen756" class=" x-btn-text">Save</button>
driver.find_element_by_xpath("//button[@id='ext-gen756'][@class=' x-btn-text']")
我不想提及像这样的序列号-

//div[@class='x-combo-list-item']/div[1]

如果您想在
xpath
中将
id
class
组合在一起,可以这样尝试-

driver.find_element_by_xpath('//button[@id="ext-gen756"][@class=" x-btn-text"]');
您也可以使用
-

driver.find_element_by_xpath('//button[@id="ext-gen756" and @class=" x-btn-text"]');
已编辑

您的
xpath
似乎不正确。使用下列内容-

driver.find_element_by_xpath('//div[@class="x-combo-list-item"][contains(.,"Global ID")]');

如果您想在
xpath
中将
id
class
组合在一起,可以这样尝试-

driver.find_element_by_xpath('//button[@id="ext-gen756"][@class=" x-btn-text"]');
您也可以使用
-

driver.find_element_by_xpath('//button[@id="ext-gen756" and @class=" x-btn-text"]');
已编辑

您的
xpath
似乎不正确。使用下列内容-

driver.find_element_by_xpath('//div[@class="x-combo-list-item"][contains(.,"Global ID")]');

很长一段时间后,我只是回答了我自己的问题。这个问题是在我不熟悉xpath主题时发布的

<button type="button" id="ext-gen756" class=" x-btn-text">Save</button>
有时,如果Id是动态的,并且每次重新加载页面时都会发生更改,则您可以尝试:

driver.find_element_by_xpath("//button[@type='Save'][contains(@id,'ext-gen')][@class=' x-btn-text']")

在这里,我使用了@type,对于@id contains选项作为前缀(ext gen),动态id的前缀通常保持不变。这个问题是在我不熟悉xpath主题时发布的

<button type="button" id="ext-gen756" class=" x-btn-text">Save</button>
有时,如果Id是动态的,并且每次重新加载页面时都会发生更改,则您可以尝试:

driver.find_element_by_xpath("//button[@type='Save'][contains(@id,'ext-gen')][@class=' x-btn-text']")

这里我使用了@type,对于@id contains选项作为前缀(ext gen),动态id的前缀通常保持不变

在类的开头有一个空格,添加它,答案将是upvoted@Narendra我已经编辑了这个问题,请看其他的section@NarendraRajput上述xpath也不起作用:(我对
python
不太了解。确保元素不在
IFrame
下,如果是,则首先需要切换框架。如果不是,则使用一些
Explicitwait
引用此内容-在类的开头有一个空白,添加它,答案将是upvoted@Narendra我已经编辑了这个问题,请看其他的 section@NarendraRajput上面提到的xpath也不起作用:(我不太了解
python
。确保元素不在
IFrame
下,如果是,则首先需要切换frame。如果不是,则使用一些
Explicitwait
参考此-