admin管理员组

文章数量:1125095

I'm using selenium in python and trying to verify if an element is visible and it's border color is green. If the statement is met, the code is sending a message from telegram. I have 7 elements (sessions) to check for each day (Monday to Sunday) and I need to put the following code in a loop. In total 49 elements to check.

"day" and "seans" (session) numbers should range from 0 to 6 (day0 to day6, and seans0 to seans6). elements to check should be like: day0_seans0, day0_seans1, day0_seans2, ..., day0_seans6, day1_seans0, day1_seans1, ...

Any suggestions?

try:
    day0_seans0 = driver.find_element(By.ID, ("pageContent_rptList_ChildRepeater_0_dvSeans_0"))
    day0_seans0_renk = day0_seans0.value_of_css_property('border-color')
    if day0_seans0.is_displayed() and day0_seans0_renk == "rgb(8, 245, 26)":
        message = 'my message'
        url = f"{TOKEN}/sendMessage?chat_id={CHAT_ID}&text={message}"
        r = requests.get(url)
        print(r.json())
    else:
        pass
except NoSuchElementException:
    pass

I'm using selenium in python and trying to verify if an element is visible and it's border color is green. If the statement is met, the code is sending a message from telegram. I have 7 elements (sessions) to check for each day (Monday to Sunday) and I need to put the following code in a loop. In total 49 elements to check.

"day" and "seans" (session) numbers should range from 0 to 6 (day0 to day6, and seans0 to seans6). elements to check should be like: day0_seans0, day0_seans1, day0_seans2, ..., day0_seans6, day1_seans0, day1_seans1, ...

Any suggestions?

try:
    day0_seans0 = driver.find_element(By.ID, ("pageContent_rptList_ChildRepeater_0_dvSeans_0"))
    day0_seans0_renk = day0_seans0.value_of_css_property('border-color')
    if day0_seans0.is_displayed() and day0_seans0_renk == "rgb(8, 245, 26)":
        message = 'my message'
        url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={CHAT_ID}&text={message}"
        r = requests.get(url)
        print(r.json())
    else:
        pass
except NoSuchElementException:
    pass
Share Improve this question edited 2 days ago Murat asked 2 days ago MuratMurat 355 bronze badges 3
  • Please clarify your question. What is going wrong with your current code, or how would you like it to change? – Alex Duchnowski Commented 2 days ago
  • Can you share the URL? I think there is likely a better/simpler way to do this without having to hard code the locators for all 7 elements. – JeffC Commented 2 days ago
  • Alex: I don't want to repeat this code for day0_seans1, day0_seans2, day0_seans3.... day7_seans7. Same goes for the "renk" (color). It should start with day0_seans0_renk, day0_seans1, and end up with day6_seans6_renk. So I believe I need a nested loop but can't figure out how to do. JefffC: The URL is online.spor.istanbul/uyegiris but it is in Turkish and you need to log-in :( I need to check 7 sessions per day, so it should check 49 elements in total. – Murat Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Do you mean to use arrays/matrix like that?

try:
    rows, cols = (7, 7)

    day_seans = [[0]*cols]*rows
    day_seans_renk = [[0]*cols]*rows
    
    for x in range(7):
        for y in range(7):
            day_seans[x][y] = driver.find_element(By.ID, ("pageContent_rptList_ChildRepeater_0_dvSeans_0"))
            day_seans_renk[x][y] = day_seans[x][y].value_of_css_property('border-color')
            if day_seans[x][y].is_displayed() and day_seans_renk[x][y] == "rgb(8, 245, 26)":
                message = 'my message'
                url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={CHAT_ID}&text={message}"
                r = requests.get(url)
                print(r.json())
            else:
                pass
except NoSuchElementException:
    pass

day_seans and day_seans_renk are matrix accessible with the indexes like

day0_seans0 -> day_seans[0][0]
day1_seans1 -> day_seans[1][1]
day0_seans1 -> day_seans[0][1]

本文标签: Selenium pythonHow to put naming an element in a loop with sequential numberingStack Overflow