1

I have a program with python & selenium. I will print the program like this :

end-bubble bubble ok

end-bubble bubble high

My goals are like this:

ok

high

My idea is to print without the end-bubble bubble only ok or high but I don't know how the script with python selenium.

Please give me solution.

This is my script :

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome (executable_path=r"C:\Users\rizki.abdillah\Desktop\Selenium\chromedriver.exe")
# maximize with maximize_window()
driver.maximize_window()
driver.get("https://status.cloud.google.com/")
# identify element
service_status = driver.find_elements_by_css_selector("tr>.day.col8>span").get_attribute("class")
print(service_status)
1
  • A more robust test would likely split the string and then verify that the class you're expecting is in the array of strings (i.e. the order of classes in CSS is not important)
    – ernie
    Apr 6 at 23:08
1

You can use Python's .split() function. Change the last line of your script to

print(service_status.split(' ')[2])
1

It will depend on what you want to tell in your code.

If you want to remove the text "end-bubble bubble", then you can use replace:

service_status.replace('end-bubble bubble','')

If you want to explicitly only take the last word of the string, you can use deconstruction:

_, _, last = service_status.split()

Or if you want to be more cryptic (don't be please):

service_status.split()[-1]
0

The strategy mostly followed by the quality assurance services is to use the language inbuilt functions/libraries while automating similar scenarios.

Here we can use ".split()" inbuilt function to break the string and then using index [1] or [2] to fetch the required text.

We will be splitting the String at 'bubble'. There are 02 occurrence of 'bubble' text so we need to be careful while splitting the String

text1 = "end-bubble bubble ok"
text2 = "end-bubble bubble high"

print(text1.split('bubble')[2]) #this will split the string at 1st bubble occurrence, so index=2 is used for printing 'ok'
print(text1.split(' bubble ')[1]) #this will split the string at 2nd bubble occurrence
text2.split(' bubble ')[1])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.