0
<td role="gridcell" style="" title="'J' AIR FILLED CYLINDERS DDDFDF ''@#" aria-describedby="item_grid_ItemCode" class="edit-cell ui-state-highlight">"J" AIR FILLED CYLINDERS DDDFDF ''@#</td>

ItemCode="J" AIR FILLED CYLINDERS DDDFDF ''@#

tried below xpath which didn't worked

1. //td[text()=\""+ItemCode+"\"]
2. //td[text()=\'"+ItemCode+"\']

3 Answers3

0

See if this works


driver.find_element_by_xpath(".//td[@class='edit-cell ui-state-highlight']").text
itronic1990
  • 1,231
  • 2
  • 4
  • 18
  • i want to find by text something like //td[text()=\""+ItemCode+"\"], i have multiple itemcode and itemcode contains singel quote or double quotes in it – Jesaji odedara Jun 02 '21 at 14:53
0

If I understand what you are trying to do, is to locate element according to it's text.
If so, please try this:

String locator = "//td[contains(text(),'%s')]";
locator = String.format(locator,"'J' AIR FILLED CYLINDERS DDDFDF ''@#");
driver.findElement(By.xpath(locator));

here you can pass any relevant value inside the format() to format a specific XPath locator

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

Your usecase is quite tricky.

Depending on which scraping language you use, maybe(I'm not able to test in the moment) use something like this (example JavaScript):

let xpath = '//td[text()=concat(\'"J" AIR FILLED CYLINDERS DDDFDF \',"\'\'\'\'@#") ]';

This wil construct this XPath:

//td[text()=concat('"J" AIR FILLED CYLINDERS DDDFDF ',"''''@#") ]

Meaning: in the case that there is a combi of single or double quotes in your ItemCode you have to build a XPath using the XPath-concat() function.

See these answers answer and this answer and this question.

Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19
  • thank you Siebe its working but doesn't solved whole problem as my string will be dynamic and string came from Excel sheet ,there are lots off itemcode string available in excel – Jesaji odedara Jun 04 '21 at 14:14
  • So you have to come up with code that in case you have both: single and double quote, that will build that concat string. Should be doable. Succes – Siebe Jongebloed Jun 04 '21 at 14:34
  • i created function to generate String like you have mentioned and if i use that generated string in concat it's wokring but if i store in one variable and use that variable inside concat its not working , i use this way => //td[text()=concat(\'"+Itemcode+"\') – Jesaji odedara Jun 09 '21 at 05:56
  • I suggest you just ask a new question with the relevant info, like the content of Itemcode, the name of your scripting-language and the code that you use to build the string. – Siebe Jongebloed Jun 09 '21 at 06:58
  • thank you i have done that-> https://stackoverflow.com/questions/67903886/how-to-write-xpath-with-concat-function-for-variable – Jesaji odedara Jun 10 '21 at 06:06