0

i'm new to python selenium package. I'm developing crawler for a bookie site.

I'm unable to click and open an image link.

My code:

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
web = 'https://odibets.com/'
driver.get(web)

driver.implicitly_wait(3)
# btn = driver.find_element_by_css_selector('span.icon')""
btn = driver.find_element_by_xpath("//a[@href='/League'] and //span[text()='League']")

# <img src="https://s3-eu-west-1.amazonaws.com/odibets/img/menu/odi-league-2.png">

# //img[@src ="https://s3-eu-west-1.amazonaws.com/odibets/img/menu/odi-league-2.png"]
# //span[text()='League']
btn.click()

I get the following exception. raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //a[@href='/League'] and //span[text()='League'] because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. (Session info: chrome=96.0.4664.45) Stacktrace: Backtrace: Ordinal0 [0x010D6903+2517251]

Attached is the snapshot code from chrome developer tools and page itself. enter image description here

enter image description here

1 Answer 1

1

Your href was /league not /League

driver.find_element_by_xpath("//a[@href='/league'] [.//span[contains(.,'League')]]").click()

This also works somehow the element wasn't clicking correctly.

elem=driver.find_element_by_xpath("(//a[@href='/league'] [.//span[contains(.,'League')]])[1]")
driver.execute_script("arguments[0].click()", elem)
Sign up to request clarification or add additional context in comments.

4 Comments

I get the following exception after running the code based on the code you provided. File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable (Session info: chrome=96.0.4664.45) Stacktrace: Backtrace: Ordinal0 [0x00CF6903+2517251] Ordinal0 [0x00C8F8E1+2095329] Ordinal0 [0x00B92710+1058576] still the link cannot open
Found a work around.
Why does it look complex, the click() method is not working on first one.
Something about the click function not working so I just used used javascript on the a tag.

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.