0

I am trying to write a list of code to automate my daily file downloading process. I am using Python Selenium for this. However, I am having trouble to locate one of the clicks.

HTMI Code as below:

<div class="card dark-blue"> <h3 data-mh="title-research" style="height: 52px;">Rated Range Prices (LT)</h3> <h4 class="disp_date">26-Jan-2021 17:47:58</h4> <div class="divider"></div> <a href="javascript:seemore('F10002');" value="F10002" class="link">See More »</a> </div>

Here is my code:

link = driver.find_element_by_xpath("//a[@href='javascript:seemore('F10002');']")

The error msg:

InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[@href='javascript:seemore('F10002');'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@href='javascript:seemore('F10002');']' is not a valid XPath expression.
  (Session info: chrome=88.0.4324.104)

Can anyone please enlighten me on this? Many thanks.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

2 Answers2

0

To locate the element See More » you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "div.card.dark-blue a.link[href*='seemore']")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//div[@class='card dark-blue']//a[@class='link' and starts-with(., 'See More')]")
    

Ideally, to locate the clickable element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.card.dark-blue a.link[href*='seemore']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='card dark-blue']//a[@class='link' and starts-with(., 'See More')]")))
    
  • 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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
-1

You're using single quotes pair inside another single quotes pair...

Try

link = driver.find_element_by_xpath('''//a[@href="javascript:seemore('F10002');"]''')
JaSON
  • 3,460
  • 2
  • 6
  • 13
  • Thanks. The syntax is correct now. However, it is still unable to locate element. HTMI Code as below:

    Rated Range Prices (LT)

    26-Jan-2021 17:47:58

    See More »
    – Edwin Soon Jan 26 '21 at 16:22
  • @EdwinSoon check whether link located inside an iframe or you might need to [wait for element to be clickable](https://selenium-python.readthedocs.io/waits.html) – JaSON Jan 26 '21 at 16:24