0

I am trying to write a code that interacts with the website Primel. The point is to guess a random five-digit prime number. If the number you guess the correct number in the correct spot, it turns green, if it is present in the prime number but not in the correct place, it turns yellow, and if it is not in the prime number entirely, it turns grey. I got the code to pick a randomly generated 5-digit prime number and press the buttons that correspond with those digits, but I got stuck when I wanted to mark which number is in the correct space etc. What I need to do is loop through is this: I want to loop through the first "flex justify-center mb-1" on the first turn, the second one on the second turn etc. and what I am trying to do is see the class each number is assigned to. For example, if the number is green, the class becomes ... border-green... The part of the code that doesn't work is this:

check_numbers = driver.find_element(By.CLASS_NAME, "flex justify-center mb-1")
for item in check_numbers:
    if driver.find_element(By.CLASS_NAME, "w-14 h-14 border-solid border-2 flex items-center justify-center mx-0.5 text-lg font-bold rounded bg-slate-400 text-white border-slate-400"):
        num_to_delete = item.text
    elif driver.find_element(By.CLASS_NAME, "w-14 h-14 border-solid border-2 flex items-center justify-center mx-0.5 text-lg font-bold rounded bg-yellow-500 text-white border-yellow-500"):
        num_in_different_index = item.text
    else:
        correct_num = item.text

I used num_to_delete and the rest as placeholders, so they are not functional at the moment, but that's not the problem. When I try the for loop, I get this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".flex justify-center mb-1"}

I have tried locating the class using XPATH, but that didn't work either. What am I doing wrong/is this even possible?

1 Answer 1

1

flex justify-center mb-1 are actually multiple class name values.
To locate web element based on these values you should use CSS_SELECTOR or XPATH, like this:

check_numbers = driver.find_element(By.CSS_SELECTOR, ".flex.justify-center.mb-1")

The same about the other elements:

for item in check_numbers:
    if driver.find_element(By.CSS_SELECTOR, ".w-14.h-14.border-solid.border-2.flex.items-center.justify-center.mx-0.5.text-lg.font-bold.rounded.bg-slate-400.text-white.border-slate-400"):
        num_to_delete = item.text
    elif driver.find_element(By.CSS_SELECTOR, ".w-14.h-14.border-solid.border-2.flex.items-center.justify-center.mx-0.5.text-lg.font-bold.rounded.bg-yellow-500.text-white.border-yellow-500"):
        num_in_different_index = item.text
    else:
        correct_num = item.text

Also, your locators are looking extremely unreliable, you should improve them.

Sign up to request clarification or add additional context in comments.

Comments

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.