0

I want to click button

<p class="btn" 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!

booboo
  • 13
  • 4

3 Answers3

0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thank you reply. and i tried , but it also failed. – booboo Feb 03 '23 at 17:38
  • WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[@class='btn' and @id='LargeNextBtn']/a[@id='LargeNextBtnLink']/img[@id='LargeNextBtnImage']"))).click() File "/Users/zhuwenping/anaconda3/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 95, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – booboo Feb 03 '23 at 17:38
0

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()
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Isn't the `LargeNextBtn` element out of the scope of ` – undetected Selenium Feb 03 '23 at 18:07
  • 1
    @undetectedSelenium, Ah, yes you are right! I have updated, snapshot is always a challenge :-D – KunduK Feb 03 '23 at 18:09
  • thank you reply. and i tried .but i also failed on pTag=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"LargeNextBtn"))) – booboo Feb 03 '23 at 23:43
  • @booboo, Don’t switch to iframe.as element is not inside the iframe. Removed the line you have added for iframe and try my code again. Hope that works. – KunduK Feb 03 '23 at 23:46
  • i didn't add any switch element.it also can not work. – booboo Feb 03 '23 at 23:50
  • Well.is it possible to share the link? – KunduK Feb 03 '23 at 23:53
  • i want dut this it a little diffcult to share link. because this link it is need to login and after click a botton. – booboo Feb 03 '23 at 23:57
  • https://www.globalinterpark.com/detail/edetail?prdNo=23001060&dispNo=01003 select a date and click the buy ticket. will show the link – booboo Feb 03 '23 at 23:58
0

The element you want is not inside the IFRAME so you don't have to switch contexts. Instead of targeting the P tag, I would instead target the A itself since it has an ID.

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "LargeNextBtnLink"))).click()
JeffC
  • 22,180
  • 5
  • 32
  • 55