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.
CodePudding user response:
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)