1

This is my first time posting in this forum, I am doing some selenium webdriver testing, I collected some data from an html UI, put it in an array:

consents = $driver.find_elements(:xpath,"//*[@id='main-display']/div[2]/div[2]/div/table/tbody/tr/td[7]//i")

, then I processed the array with map :

consent_values = consents.map { |consent| consent.attribute('class')}

printing the array I got :

["fa fa-check-circle", "fa fa-circle-thin", "fa fa-check-circle", "fa fa-circle-thin", "fa fa-circle-thin", "fa fa-check-circle", "fa fa-circle-thin", "fa fa-circle-thin", "fa fa-check-circle,....]

1) Is there a way to do in the map to remove "fa" from all strings in the array ?

2) How about changing the strings "fa fa-check-circle" to be true and "fa fa-circle-thin" to be false so I will get : [true,false,true,....]

Thanks,

3
  • 1) One way to do it is by using String#sub with a Regexp as pattern. 2) What prevents you do that? Commented Feb 27, 2018 at 14:29
  • I got it, I used string[N..-1] , was wondering if can change the strings to true or false in the map transformation. Commented Feb 27, 2018 at 14:55
  • There's a thread here that goes into detail on removing the start of a string. Might find it good reading. Commented Feb 27, 2018 at 15:36

2 Answers 2

1

Basically 2) eliminates the necessity of 1), so here you go:

consent_values = consents.map do |consent|
  case consent.attribute('class')
  when "fa fa-check-circle" then true
  when "fa fa-circle-thin" then false
  else nil
  end
end

To remove "fa " prefix from the string one might use String#[]:

"fa fa-check-circle"[/(?<=\Afa ).*/]
#⇒ "fa-check-circle"
Sign up to request clarification or add additional context in comments.

3 Comments

I like this answer too, I will go try it.
@Fatna This one has the clear answer for you because you can completely eliminate one step.
Didn't know you could do that with String#[]. Informative as ever, +1. (I also like 2.5.0's delete_prefix for readability.)
0
string[N..-1]

This worked for me. thanks anyway, I hope I will interact with this forum lot.

1 Comment

Hi, Have a look at @mudasobwa 's answer, he has written an answer which actually reduces one step for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.