Home > Blockchain >  How inspect element and find the correct element? I'm using Python and Selenium
How inspect element and find the correct element? I'm using Python and Selenium

Time:12-08

I need access the menu and select an option using find_element_by_css_selector, find_element_by_name or driver.find_elements_by_id("#####").click() but it doesn't work, I think it's because have more than an option to select.

Here an example and below the code:

enter image description here

#Botão de mostrar descrição da cotação "hidden"
driver.find_elements_by_id("Mostrar412659").click()

CodePudding user response:

I noticed that there is a typo in your code.

Your code:

driver.find_elements_by_id("Mostrar412659").click()

Right code:

driver.find_element_by_id("Mostrar412659").click()

Answer:

You have to expand the select tag in order to see the options. Once you do that, you can right click on the option and copy whaterver you need to find the element.

See the picture as a example.

enter image description here

Bear in mind that a lot of times it's better to use the WebDriverWait class to click on a element. Like:

from selenium.webdriver.support.wait import WebDriverWait

elem = WebDriverWait(driver, 30).until(
    EC.element_to_be_clickable((By.ID,'Cotacoes')))
elem.click()

Sample code

driver.find_element_by_id('Cotacoes').click()
driver.find_element_by_xpath('//*[@id="Cotacoes"]/option[1]').click()

Sample code 2

elem = WebDriverWait(driver, 30).until(
    EC.element_to_be_clickable((By.ID, 'Cotacoes')))
elem.click()

elem = WebDriverWait(driver, 30).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="Cotacoes"]/option[1]')))
elem.click()

!! Change the XPATH and ID values to your ID and XPATH !!

Lemme know if you have any question about it!

  • Related