pythonseleniumselenium-webdriverselenium-chromedrivergoogle-trends

Selenium - Not able to get xpath in dynamic element


Im trying to get the xpath of an element in Google Trends that seems to be dynamic causing a strange reload in the console which doesnt allow me to get the path. Because of this Ive also tried selecting by an id I saw but still not working.

What im trying to do is to add a comparison query in the search box with a title "add a search term" (after the first click to this same element).

Heres an example url: https://trends.google.com/trends/explore?q=python%20programming&geo=US

Is it perhaps that i need to wait? Im puzzled with the hidden html when i try to inspect in the console.

# click to add and compare query
driver.find_element_by_xpath('//*[@id="explorepage-content-header"]/explore-pills/div/button/span/span[1]').click()
time.sleep(10)

# find comparisson search box
driver.maximize_window() 
driver.implicitly_wait(20) 
ele = driver.find_element_by_id('input-139')
time.sleep(1)

ele.send_keys('r programming') <-- im not able to add this query in the comparison box
ele.send_keys(Keys.RETURN)

This is the error message.

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="input-139"]"}
  (Session info: chrome=81.0.4044.138)

Solution

  • The page seems to be generating different IDs during a page reload and on different browsers. I'm assuming this is most likely related to the fact that the page is using angular.

    I've used the following code and was able to get it to work, but I assumed that we're always going to be entering into the second search box. The first search box being the original term.

    search_boxes = driver.find_elements_by_css_selector('input[aria-label="Add a search term"]')
    target_box = search_boxes[1] # Second Box, we're assuming there is always one term.
    
    target_box.send_keys('r programming')
    target_box.send_keys(Keys.RETURN)