Home > Blockchain >  Use driver.find_element("id","") can not find
Use driver.find_element("id","") can not find

Time:02-04

I want to click button

<p  id="LargeNextBtn" style=""><a href="javascript:fnNextStep('P');" id="LargeNextBtnLink" onfocus="this.blur();"><img src="//ticketimage.globalinterpark.com/ticketimage/Global/Play/onestop/G2001/btn_next_on.gif" id="LargeNextBtnImage" alt=""> </a></p>

but when I use find_element

driver.find_element("id", "LargeNextBtn").click()

can't find "LargeNextBtn"

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="LargeNextBtn"]"}

I also try to into iframe but it also fail

WebDriverWait(driver, 9).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"ifrmBookStep")))

enter image description here

Any help or pointers is appreciated, thank you!

CodePudding user response:

To click on the element you can use either of the following locator strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "p.btn#LargeNextBtn > a#LargeNextBtnLink > img#LargeNextBtnImage").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//p[@class='btn' and @id='LargeNextBtn']/a[@id='LargeNextBtnLink']/img[@id='LargeNextBtnImage']").click()
    

Ideally to click on the clickable element instead of presence_of_element_located() 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, "p.btn#LargeNextBtn > a#LargeNextBtnLink > img#LargeNextBtnImage"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[@class='btn' and @id='LargeNextBtn']/a[@id='LargeNextBtnLink']/img[@id='LargeNextBtnImage']"))).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
    

CodePudding user response:

You need to identify the p tag.

You also need to change the style attribute to display : block use javascripts executor to change the style of the element.

Code:

pTag=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"LargeNextBtn")))
driver.execute_script("arguments[0].style.display = 'block';", pTag)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a#LargeNextBtnLink"))).click()
  • Related