0

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Hamza Arshad
  • 121
  • 1
  • 3
  • 11
  • Is the page loaded when the commands are invoked? If not, there are a few ways to let selenium wait, until the page is loaded. For example: https://stackoverflow.com/questions/26566799/wait-until-page-is-loaded-with-selenium-webdriver-for-python – Futureflo Aug 09 '20 at 19:53
  • Yes its fully loaded ..it types in gold but doesn't move onto the URL its supposed to go to ..instead it just stays on the main page of the website that it started on – Hamza Arshad Aug 09 '20 at 20:14

2 Answers2

1

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"
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • nope didnt work ..more or less the same results but when i did the second xpath way the tab closes instantly after its ran ..i don't even know if it actually clicked on gold or not. – Hamza Arshad Aug 09 '20 at 21:01
  • @HamzaArshad You didn't provide the HTML, hence the answer was a pure guesswork based on the information you have provided. However, I'm sure you don't see `illegalselector` any more. – undetected Selenium Aug 10 '20 at 10:55
0

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()
E.Wiest
  • 5,425
  • 2
  • 7
  • 12