-2

I want to verify if the image name contains certain text or not. Whats the best way to do it

I have attached an image for reference as below:

enter image description here

dbc
  • 91,441
  • 18
  • 186
  • 284
Nida
  • 1
  • 1
  • 3
  • Do you mean the src or the alt? – Thomas Cook Sep 20 '17 at 14:54
  • Image name in src – Nida Sep 20 '17 at 14:57
  • You can use JQuery to check the if the value of either of these attributes contains "5.0" like follows: $("img[src*='5.0']") This will get you all img elements from the dom whose src value contains the string "5.0". – Thomas Cook Sep 20 '17 at 15:00
  • If you want to check if that specific element has a src which contains "5.0" you'll need to get that element on it's own first, to do so you'll need to give your element an id or use a combination of JQuery selectors to select it via unique key (for instance you might select it via it's alt text). I would suggest adding an id to the element. – Thomas Cook Sep 20 '17 at 15:02
  • See: [How do I do X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do-x) The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – JeffC Sep 20 '17 at 15:27
  • Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Sep 20 '17 at 15:27

1 Answers1

-1

One way you could do this (using js) is with the .indexOf method. In your case, you could use code like this:

// If you gave your img element an id="picture",
// you could get your src text with this line:
var imageSource = document.getElementById("picture").src;

// boolean for whether or not 5.0 exists in the source
var exists = false;

// indexOf will return the index that 5.0 is located at
// if it does not exist, it will return -1
// here, I evaluate if the index of 5.0 is not -1
// if it is not -1, that means it does exist 
if (imageSource.indexOf("5.0") != -1)
     exists = true;

Here is the w3schools link for the .indexOf method: https://www.w3schools.com/jsref/jsref_indexof.asp

Yes, this is JavaScript. Which you CAN use: Execute JavaScript using Selenium WebDriver in C#

Hope this helps, good luck!

Austin VB
  • 36
  • 5
  • @JeffC https://stackoverflow.com/questions/6229769/execute-javascript-using-selenium-webdriver-in-c-sharp – Austin VB Sep 20 '17 at 15:41
  • I know how to run JS in C#. Your answer here doesn't cover that. See [this](https://meta.stackoverflow.com/questions/290046/if-the-question-is-specifically-about-a-certain-language-is-an-answer-in-anothe). – JeffC Sep 20 '17 at 16:13
  • @JeffC You should look at the answer from Dennis Jaheruddin from the link you just sent me. – Austin VB Sep 21 '17 at 13:52
  • @JeffC I will be honest and say I have not worked with Selenium WebDriver. However I do know C# very well but didn't want to assume normal C# code would work. Because no one had answered, I figured I would do my best to solve the problem. Can't blame a guy for trying. Instead of trying to run me down you could always answer the question? – Austin VB Sep 21 '17 at 14:02
  • I used to do the same thing (give answers in a language other than that requested) when I was learning the site, then others educated me on the site rules. While I may not always agree with them, I try to follow them. My intention was not to "run you down", it was to inform you of the site rules. – JeffC Sep 21 '17 at 15:14
  • I know the answer but the reason I haven't answered is that the OP needs to learn the site rules. They need to [ask a good question](https://stackoverflow.com/help/how-to-ask) that meets the acceptable criteria for SO. I feel like if I answer a poor question, then I've taught someone else that it's OK to ask poor questions. Making them go through the process of learning how to ask a good question and fixing their question to fit those criteria makes SO better. THEN... I will answer the question. It's a longer process in the short term but in the long term it makes the site better. – JeffC Sep 21 '17 at 15:17