I am trying to click a button on a website using Selenium.
Below are my attempts. None have worked.
#CSS Selector
driver.find_element_by_css_selector(".more-info-button").click()
driver.find_element_by_css_selector("more-info.span.more").click()
driver.find_element_by_css_selector("more-info > span.more").click()
driver.find_element_by_css_selector("span.more").click()
driver.find_element_by_css_selector("#more-info").click()
driver.find_element_by_css_selector(".more").click()
#ClassName
driver.find_element_by_class_name("more-info-button").click()
driver.find_element_by_class_name("More").click()
driver.find_element_by_class_name("more-info-button.trigger").click()
#Xpath
driver.find_element_by_xpath("//span[contains(text(),'More')]").click()
driver.find_element_by_xpath("//font[contains(text(),'MORE INFO')]").click()
driver.find_element_by_xpath("/html/body/div[3]/div[4]/span[1]").click()
driver.find_element_by_xpath("//span[@id='more-info']/span").click()
driver.find_element_by_xpath("//span/span").click()
driver.find_element_by_xpath("//span[@id='more-info']").click()
driver.find_element_by_xpath("//div[@id='panel']/div[4]/span").click()
driver.find_element_by_xpath("//div[4]/span").click()
Screenshot of HTML:
CodePudding user response:
Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.
xpath that you should check :
//span[@id='more-info']
CSS that you should check :
span#more-info
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL F
-> then paste the xpath/css
and see, if your desired element
is getting highlighted with 1/1
matching node.
Now if it's unique try to click using one of the below technique:
Code trial 1:
time.sleep(5)
driver.find_element(By.XPATH, "//span[@id='more-info']").click()
Code trial 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='more-info']"))).click()
Code trial 3:
time.sleep(5)
button = driver.find_element(By.XPATH, "//span[@id='more-info']")
driver.execute_script("arguments[0].click();", button)
Code trial 4:
time.sleep(5)
button = driver.find_element(By.XPATH, "//span[@id='more-info']")
ActionChains(driver).move_to_element(button).click().perform()
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
CodePudding user response:
The element MORE INFO is represented within the HTML DOM as follows:
<span id="more-info" >
<span >More</span>
<span >Hide</span>
" Info"
</span>
Solution
To click() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.more-info-button.trigger#more-info span.more"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='more-info-button trigger' and @id='more-info']//span[@class='more']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC