0

I want to delete "Apple" and enter "Google", and then click the "Search" button. This is the url: https://www.glassdoor.ca/Reviews/apple-reviews-SRCH_KE0,5.htm

enter image description here

The company input box tag: Company Input Box

The search button tag: Search Button

I'm using the following code:

element = browser.find_element_by_xpath('//input[@id="sc.keyword"]')
element.send_keys(Keys.CONTROL + "a")
element.send_keys(Keys.DELETE)
element.send_keys("Google")
browser.find_element_by_xpath('//button[@type="submit"]').click()

But nothing happens. Does anyone know why? Thank you very much!

[NEW] I just found that if I skip the sign-in step, then the code is able to change the company name.

[LATEST DEVELOPMENT] When I added a line of code to click the "Search" button after sign-in, it works. Thank you, everyone.

The full code is as follows:

def open_default_page(target_folder):
    default_url = r"https://www.glassdoor.ca/Reviews/apple-reviews-SRCH_KE0,5.htm"
    browser.implicitly_wait(20)
    browser.maximize_window()
    browser.get(default_url)
    time.sleep(1)
    return browser

    
def enter_company(com_name):
# Enter the keyword:

    element = browser.find_element_by_xpath("//input[@placeholder='Company 
              Name']")

    element.send_keys(Keys.CONTROL + "a")
    element.send_keys(Keys.DELETE)
    element.send_keys(com_name)

    time.sleep(1)
    # click Search
    browser.find_element_by_xpath('//button[@type="submit"]').click()
    time.sleep(2)


def sign_in(browser):
    some code
    return browser

# Open the default page
target_folder = r"D:\somefolder"
browser = open_default_page(target_folder)
browser = sign_in(browser)


def main():
    sign_in(target_folder)
    enter_company("Google")
    # followed by some other further steps

if __name__ == "__main__":
   main()
2
  • may be try .send_keys("") Commented Mar 13, 2021 at 6:28
  • Doesn't work. Thanks. Commented Mar 13, 2021 at 17:25

2 Answers 2

2

You can try element.clear(). Here is the documentation

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

3 Comments

Can you try this out ? Wait for the element to be clickable, click on it and then try to clear the text. element = WebDriverWait (driver, 10).until(EC.element_to_be_clickable((By.Xpath, //input[@id="sc.keyword"]))) element.click() element.clear()
Following your code, got the following error message: selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
I printed the status before "click", and find that the element is visible: print("Element is visible? " + str(element.is_displayed()))
1

Cannot comment on your post so putting my suggestions here.

Just tried your example "as-is" and it worked well without any changes. Just used Chrome:

browser = webdriver.Chrome()
browser.get('https://www.glassdoor.ca/Reviews/apple-reviews-SRCH_KE0,5.htm')

4 Comments

@VictorWang the most likely script failed with some exception. Could you please share all the execution output?
Probably because of the way I packaged some of the codes into a function. When I put all the codes outside of a function. They are working fine.
Each part works fine. When I put them together into a function. No error message but the company name is not changed.
I figured it out. The issue lies in the sign-in step. After sign-in, I clicked the "Search" button to refresh the page and it's fine. I first load a default as an easy alternative to setting the default search type, i.e. companies.

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.