xpath

generate/get the Xpath for a selected textnode

夙愿已清 提交于 2020-01-23 09:32:11
问题 Is it possible to get/generate the XPath, for a text selected in the HTML page. Say,I have opened an html file using a browser,say safari/firefox, now i select some text (using mouse), i want to generate/get the xpath for that. is it possible to get it programatically? how? 回答1: Some years ago I wrote the following sample and tested it to work with Mozilla and with Opera. It stores the selection in a cookie and can restore the selection from that cookie. It generates and stores an XPath

Searching the DOM for multiples of the same string, using XPath

会有一股神秘感。 提交于 2020-01-23 01:01:28
问题 I'm writing a Chrome extension that will search the DOM and highlight all email addresses on the page. I found this to look for at symbols on the page but it only returns correctly when there is one email address, it breaks when there are multiple addresses found. found = document.evaluate('//*[contains(text(),"@")]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0); What is the correct way to have this return multiples if more than one is found? 回答1: If you want

Create XPath expression programmatically

*爱你&永不变心* 提交于 2020-01-22 15:16:06
问题 Is it possible to generate the most specific XPath expression automatically from the position of the cursor on the web page? The XPath expression would change with "onMouseMove event". If it's possible, how would you implement it? Or is it already implemented in some Javascript or Python library? I would prefer it in Python with a combination of some web library but Javascript would be good and acceptable too. 回答1: See the Get XPath thread in DZone Snippets for finding the XPath. See the How

Error 'Unable to locate element' when trying to locate 'Add to Cart' button on Flipkart

非 Y 不嫁゛ 提交于 2020-01-22 03:08:11
问题 I am trying to locate Add to Cart button on Flipkart but it does not work I have tried below xpaths but none works By AddToCart= By.xpath("//button[@class='_2AkmmA _2Npkh4 _2MWPVK'][text()='ADD TO CART']"); OR By AddToCart= By.xpath("//button[text()='ADD TO CART']"); //error org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='_2AkmmA _2Npkh4 _2MWPVK'][text()='ADD TO CART']"} 回答1: Try this one. By CSS Selector --

XPath recursive “parent”-selection in a flat structure

孤街醉人 提交于 2020-01-22 00:25:32
问题 The following XML is given: <root> <element> <id>1</id> </element> <element> <id>2</id> <parentId>1</parentId> </element> <element> <id>3</id> <parentId>2</parentId> </element> <element> <id>4</id> <parentId>3</parentId> </element> <element> <id>5</id> <parentId>2</parentId> </element> <element> <id>6</id> <parentId>5</parentId> </element> </root> Now, I want to select all "parent"-nodes for e.g. element 3. Let's say, the desired output for element 3 should be: element 1 element 2 The desired

XPath recursive “parent”-selection in a flat structure

六月ゝ 毕业季﹏ 提交于 2020-01-22 00:24:10
问题 The following XML is given: <root> <element> <id>1</id> </element> <element> <id>2</id> <parentId>1</parentId> </element> <element> <id>3</id> <parentId>2</parentId> </element> <element> <id>4</id> <parentId>3</parentId> </element> <element> <id>5</id> <parentId>2</parentId> </element> <element> <id>6</id> <parentId>5</parentId> </element> </root> Now, I want to select all "parent"-nodes for e.g. element 3. Let's say, the desired output for element 3 should be: element 1 element 2 The desired

电影天堂小爬虫(xpath练习)

爷,独闯天下 提交于 2020-01-21 23:47:53
1 # _author: Jolly 2 # date: 2019/8/28 3 4 import requests 5 import time 6 from lxml import etree 7 8 9 BASE_DOMAIN = 'https://dytt8.net' 10 dytt8_url = 'https://www.dytt8.net/html/gndy/dyzz/list_23_1.html' 11 HEADERS = { 12 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', 13 'Referer': 'https://www.dytt8.net/html/gndy/dyzz/list_23_2.html' 14 } 15 response = requests.get(dytt8_url, headers=HEADERS) 16 html = etree.HTML(response.text) 17 # print(etree.tostring(html, encoding='utf-8').decode('utf-8')) 18 def get

How to get values from an XML file matching XPath query in C#

孤街醉人 提交于 2020-01-21 20:00:34
问题 I'm wondering whether there is a way using C# which enables me to return all the inner values within an XML file matching a given XPath query. Let's suppose that we have the following Xml file named exampleWithFruits.xml: <fruits> <bananas> <banana id="1" color="yellow" price="0.5" /> <banana id="2" color="yellow" price="0.4" /> <banana id="3" color="yellow" price="0.6" /> </bananas> <apples> <apple id="1" color="red" price="0.5" /> <apple id="2" color="red" price="0.4" /> <apple id="3" color

How to get values from an XML file matching XPath query in C#

不问归期 提交于 2020-01-21 19:57:17
问题 I'm wondering whether there is a way using C# which enables me to return all the inner values within an XML file matching a given XPath query. Let's suppose that we have the following Xml file named exampleWithFruits.xml: <fruits> <bananas> <banana id="1" color="yellow" price="0.5" /> <banana id="2" color="yellow" price="0.4" /> <banana id="3" color="yellow" price="0.6" /> </bananas> <apples> <apple id="1" color="red" price="0.5" /> <apple id="2" color="red" price="0.4" /> <apple id="3" color

How to find element by part of its id name in selenium with python

こ雲淡風輕ζ 提交于 2020-01-21 19:17:08
问题 I'm using selenium with python,now I want to locate an element by part of its id name,what can I do? For example,now I've already located a item by id name coption5 : sixth_item = driver.find_element_by_id("coption5") Is there anyway I can locate this element only by using coption ? 回答1: To find the element which you have located with: sixth_item = driver.find_element_by_id("coption5") To locate this element only by using coption you can use can use either of the following Locator Strategies: