2

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)
Mate Mrše
4,1194 gold badges25 silver badges51 bronze badges
asked Apr 6, 2021 at 10:57
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) Commented Apr 6, 2021 at 23:08

3 Answers 3

2

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]
answered Apr 6, 2021 at 15:28
1

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

print(service_status.split(' ')[2])
answered Apr 6, 2021 at 13:33
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])
answered Jul 21, 2021 at 8:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.