Issue
I'm try to code with python , using selenium to do search and click the button. my code as below:
from selenium import webdriver
import time
path = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(path)
driver.get("https://hopamviet.vn")
#import them
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.common.by import By
########
# driver.get('http://codepad.org')
# #click radio button - select python
# python_button = driver.find_element_by_xpath("//input[@name='lang' and @value='Python']")
# python_button.click()
# #send text "print('hello world')"
# text_area = driver.find_element_by_id('textarea')
# text_area.send_keys("print('Hello World')")
# #click submit button
# submit_button = driver.find_elements_by_xpath('//*[@id="editor-form"]/table/tbody/tr[3]/td/table/tbody/tr/td/div/table/tbody/tr/td[3]/button')[0]
# submit_button.click()
search = driver.find_element_by_name("song")
search.send_keys("Hello")
search.send_keys(keys.RETURN)
#click search
submit_button = driver.find_elements_by_xpath('/html/body/nav/div/form/div/span/button')
submit_button.click()
for search button , I'm using "find_elements_by_xpath" method to select xpath then click(). but it didn't give me the result as i expected. Can you please help look ? below is HTML code:
Solution
The above line of code
#click search
submit_button = driver.find_elements_by_xpath('/html/body/nav/div/form/div/span/button')
submit_button.click()
is not certainly true, cause you are using find_elements
which will return a list in python.
Instead use find_element
#click search
submit_button = driver.find_element_by_xpath('/html/body/nav/div/form/div/span/button')
submit_button.click()
Also, the xpath is absolute, try to use relative xpath like this
//i[contains(@class,'search')]//parent::button
full code with explicit waits :
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://hopamviet.vn")
search = wait.until(EC.visibility_of_element_located((By.NAME, "song")))
search.send_keys("Hello")
#click search
submit_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//i[contains(@class,'search')]//parent::button")))
submit_button.click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.