Hamza Arshad
Hamza Arshad

Reputation: 141

illegalselector was specified or could not find xpath error using Selenium to search for element

Trying to build a small bot to help with trading but for some reason the code gets an array of slightly different errors when trying to search for a instrument.

keyboard = Controller()
self.driver = webdriver.Chrome()
self.driver.get("https://app.libertex.com/")
self.driver.find_element(By.XPATH, "//input[@value=\'\']").click()
self.driver.find_element(By.CSS_SELECTOR, ".active > input:nth-child(2)").send_keys("gold")
self.driver.find_element(By.CSS_SELECTOR, "css=.active > .link-to-profile > .a-btn").click() #*tried doing CSS_Selector and the Xpaths ,xpath=//input  ,xpath=//header[@id='region-header']/div/div/div[3]/input  each giving different errors 
keyboard.press(Keys.ENTER) 
keyboard.release(Keys.ENTER)

Each giving different errors such as:

illegalselector was specified or could not find xpath

I'm just confused as the SeleniumIDE version works happily with no problems.

Iv tried doing both the CSS selector way, Xpath way and mimicking key pressing.Nothing seems to be working.

Also on a side note The entire code visually shows the process. Is it more demanding and slower this way than if the code just ran in the back with no visuals ..and if so please could you recommend a tool that could do this

selenium download command to run in cmd is

pip install selenium

Upvotes: 0

Views: 51

Answers (2)

E.Wiest
E.Wiest

Reputation: 5905

With the following imports to add some expected conditions :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

You can send your request into the search box with :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//input[@placeholder])[1]"))).send_keys("gold")

And click on the first result with :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//div[contains(@class,'a-btn')])[1][not(ancestor::div[@id='region-suggested-instruments'])]"))).click()

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193088

All the Locator Strategies within the line:

self.driver.find_element(By.CSS_SELECTOR, "css=.active > .link-to-profile > .a-btn").click() #*tried doing CSS_Selector and the Xpaths ,xpath=//input  ,xpath=//header[@id='region-header']/div/div/div[3]/input  each giving different errors 

seems abit off.

  • Instead of By.CSS_SELECTOR, "css=.active > .link-to-profile > .a-btn" you need to use:

    By.CSS_SELECTOR, ".active > .link-to-profile > .a-btn"
    
  • Instead of xpath=//input you need to use:

    By.XPATH, "//input"
    
  • Instead of xpath=//header[@id='region-header']/div/div/div[3]/input you need to use:

    By.XPATH, "//header[@id='region-header']/div/div/div[3]/input"
    

Upvotes: 1

Related Questions