0

I am writing a functions that searches inside a website using a name_parameter, but I am sure I am misspelling something and I cannot get the correct button with my input. I know the XPATH has the correct structure because if I hard code the page_name then I get the expected result

def change_user(self, page_name):
    select_user = WebDriverWait(self.driver, 4).until(
                EC.element_to_be_clickable((
                    By.XPATH, f'//div/div/div/span[contains(text(), {0} )]'.format(page_name)))
            )
            time.sleep(random.uniform(0.15, 0.5))
    select_user.click()

A hard coded actually working version of this function is the following:

def change_user(self, page_name):
        select_user = WebDriverWait(self.driver, 4).until(
                    EC.element_to_be_clickable((
                        By.XPATH, f'//div/div/div/span[contains(text(), "Carl Rogers" )]'))
                )
                time.sleep(random.uniform(0.15, 0.5))
        select_user.click()

I am using this reference for the syntax https://www.w3schools.com/python/ref_string_format.asp

2 Answers 2

2

Try something like

path = r'//div/div/div/span[contains(text(), "{}" )]'.format('test')
Sign up to request clarification or add additional context in comments.

1 Comment

This one worked great! I did a small addition to your answer
1

You can either use the f"String {variable}" or "String {0}".format(variable) but you are using both. Just remove the f at the start of your string.

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.