Home > front end >  Finding Element with Random ID [Python/Selenium]
Finding Element with Random ID [Python/Selenium]

Time:11-02

What the problem is:
I'm new to Selenium and I'm trying to find an element on a webpage. The background is I want to write a script so that it can help me create instance on Oracle cloud. On the page where I have to select Compartment, I need to input my root name. I just can't find the input box. The webpage with code I beleive the element is the one highlighted, corresponding to the "Choose a compartment" on the left side of the webpage, but the id is random.

What I tried:
To name a few, I tried

driver.find_element(By.XPATH, '//*[@id="active_compartment_select-dc369079-e95a-2176-f8e4-6ee9c16fe105_trigger"]/ul/li/input')

driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/aside/div[3]/div/div[2]/div[1]/div/div/div/a/ul/li/input')

I also tried
By.CLASS_NAME, 'dropdown-trigger arrow bottom', it showed nothing;
By.XPATH, //div[contains(@id, 'active_compartment_select')], it showed nothing.
I even tried find_elements(By.TAG_NAME, 'input') and there's still no such element in the returned list.
I tried basically all the suggestions I could find on stackoverflow, but it still shows no such element exception.

Any suggestions? Thanks in advance!

CodePudding user response:

You can use 'starts-with' in XPath:

driver.find_element(By.XPATH, ".//*[starts-with(@id,'active_compartment_select-')]//a")

CodePudding user response:

You are trying to find the dropdown "Compartment", right?
You can find it using this xpath: driver.find_element(By.XPATH,'//input[@placeholder="Choose a compartment"]')

  • Related