Reputation: 4439
I've followed the code here: Selenium Python get_element by ID failing
but it's still not working
Here is my code
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
BLACKBERRY_UA = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+"
opts = Options()
opts.add_argument("user-agent={0}".format(BLACKBERRY_UA))
root_url = 'http://www3.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx'
ticker = '700'
driver = webdriver.Chrome(chrome_options=opts)
driver.get(root_url)
# 1 enter ticker
stock_code = WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.ID, "ct100_txt_stock_code")))
stock_code.click()
stock_code.send_keys(ticker)
#2 click on "Headline Category" radio button
button = WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.ID, "ct100_rbAfter2006")))
button.click()
#3 choose "Announcements and Notices" from drop down
driver.find_element_by_xpath("//select[@id='ct100_sel_tier_1']/option[text()='Announcements and Notices']").click()
# 4 click on "Search"
driver.find_element_by_xpath('//a[@href = \\"javascript: if (preprocessMainForm() == true ) document.forms[0].submit()"\\]').click() # click on search buttom
I've tested each one of these 4 steps and none of them work. I've also tried By.NAME
and it still doesn't work. I'm getting a TimeoutException
. Not sure if selenium is finding the element or not, why the time out?
Upvotes: 1
Views: 2116
Reputation: 1119
It's timing out because the elements can't be found. If you manually look at the html, you can see that the id's are different than what you're using.
For example you have:
stock_code = WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.ID, "ct100_txt_stock_code")))
which should be:
stock_code = WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.ID, "ctl00_txt_stock_code")))
Look at the ID value. It's a lower case L instead of a 1 at the 'ctl100' part of the ID. Check the others and just do a copy and paste from the html ID's to your code.
This works for the first 3 steps, but for step 4 use this:
driver.find_element_by_xpath("/html//form[@id='aspnetForm']/table//a[@href='javascript: if (preprocessMainForm() == true ) document.forms[0].submit();']").click() # click on search buttom
Upvotes: 2