0

I'm trying to select the first value of a list (北京 in this case) from the website: https://search.jd.com/Search?keyword=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&qrst=1&wq=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&stock=1&page=5&s=121&click=1

The xpath is:

//*[@id="ttbar-mycity"]/div[2]/div[2]/div/div/div[1]/a

My code is:

browser.find_element_by_xpath("//ul[@id='ttbar-mycity']/div[2]/div[2]/div/div/div[1]/a[.= '北京']").click()

But it throws a "No such element exception"

this is the html: enter image description here

UPDATE: this is the full code so far (thanks DebanjanB):

browser.get("https://global.jd.com/")
browser.find_element_by_id("key").send_keys(mysearch)
browser.find_element_by_id("search-btn").click()
wait.until(EC.title_contains(mysearch))
ActionChains(browser).move_to_element(WebDriverWait(browser, 50).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='ui-areamini-text' and @title='北京']")))).perform()
WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dd dorpdown-layer']//div[@class='ui-areamini-content-list clearfix']/div/a"))).click()

It throws a TimeoutException at the line

ActionChains(browser).move_to_element(WebDriverWait(browser, 50).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='ui-areamini-text' and @title='北京']")))).perform()

1 Answers1

0

To click on the first value within the list 北京 you need to:

  • Mouse Hover the element with text as 北京 inducing WebDriverWait for visibility_of_element_located()

  • Then click on the first element inducing WebDriverWait for element_to_be_clickable()

  • You can use the following Locator Strategies:

    browser.get('https://search.jd.com/Search?keyword=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&qrst=1&wq=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&stock=1&page=5&s=121&click=1')
    ActionChains(browser).move_to_element(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='ui-areamini-text' and @title='北京']")))).perform()
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dd dorpdown-layer']//div[@class='ui-areamini-content-list clearfix']/div/a"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

search_jd_com


Reference

You can find a detailed discussion on NoSuchElementException in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thank you very much for the detailed and resourceful answer. I tried running the code the but the 2nd line (move to element) throws a timeout. – Gustavo Moreno Jul 03 '20 at 21:43
  • @GustavoMoreno Even I got _Timeoutexception_ with **5** seconds initially as the site is JS based, so I had to increase the time span to **20** seconds, now it works perfect at my end. Possibly you have to go beyond that incase of slow network. – undetected Selenium Jul 03 '20 at 21:47
  • I increased to 600 and still got a timeout...the rest of the page works very fast, is it possible that there's another issue? – Gustavo Moreno Jul 03 '20 at 22:01
  • @GustavoMoreno Can you just copy paste the code and execute and let me know the status without any modification, bcuz the same code works flawless at my end. – undetected Selenium Jul 03 '20 at 22:03
  • @GustavoMoreno Can you update the main question with your current code trial and the complete error stack trace please? – undetected Selenium Jul 03 '20 at 22:08
  • @GustavoMoreno That wasn't my logic sequence :) I think I was offered the landing url as `https://search.jd.com/Search?keyword=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&qrst=1&wq=%E5%AE%A0%E7%89%A9%E9%A3%9F%E5%93%81&stock=1&page=5&s=121&click=1`. Can you just copy-paste-test the code from my answer only? – undetected Selenium Jul 03 '20 at 23:04
  • yeah, I did just copy and paste it also, this is just the solution in context. – Gustavo Moreno Jul 03 '20 at 23:07
  • @GustavoMoreno Waiting after `get()` and waiting after `search` is different. I have to have a relook tomorrow. – undetected Selenium Jul 03 '20 at 23:09