skip to Main Content

Basically, I’m trying to get the program to find the keyword, and if it manages to do it, it will click the link to the pdf file in the page (for now i just want to print a confirmation message)

But, despite the keyword clearly being in the page, selenium can’t manage to find it.

Here’s the block of code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()


url = 'https://www.bursamalaysia.com/market_information/announcements/company_announcement/announcement_details?ann_id=3434107'
driver.get(url)

try:
    
    WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.TAG_NAME, "body"), "Annual Audited Accounts"))

    print("Found: Annual Audited Accounts")
except:
    print("Text not found")


driver.quit()

I’ve tried all sorts of search methods, including XPath and JavaScript, but I literally can’t search for it.

I’m currently using Selenium on PyCharm fyi

2

Answers


  1. It’s Because the keyword Annual Audited Accounts is part of an iframe which selenium cannot access directly, you need to first switch to the iframe

    enter image description here

    add below code to swtich to ifrma first driver.switch_to.frame(driver.find_element(By.NAME,’announcement_detail_iframe’))

    Full Code

    import time
    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import NoSuchElementException
    from webdriver_manager.chrome import ChromeDriverManager
    
    # Initialize WebDriver
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    driver.implicitly_wait(10)
    
    url = 'https://www.bursamalaysia.com/market_information/announcements/company_announcement/announcement_details?ann_id=3434107'
    driver.get(url)
    
    try:
        driver.switch_to.frame(driver.find_element(By.NAME,'announcement_detail_iframe'))
        WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.TAG_NAME, "body"), "Annual Audited Accounts"))
    
        print("Found: Annual Audited Accounts")
    except:
        print("Text not found")
    
    
    driver.quit()
    

    Prints

    Found: Annual Audited Accounts
    
    Login or Signup to reply.
  2. You just need to handle the IFRAMES

    Check the working code below:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    
    url = 'https://www.bursamalaysia.com/market_information/announcements/company_announcement/announcement_details?ann_id=3434107'
    driver.get(url)
    wait = WebDriverWait(driver,10)
    
    try:
        # Below line will switch into the IFRAME
        wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "bm_ann_detail_iframe")))
        wait.until(EC.text_to_be_present_in_element((By.TAG_NAME, "body"), "Annual Audited Accounts"))
        print("Found: Annual Audited Accounts")
    except:
        print("Text not found")
    
    # Below line will come out of the IFRAME
    driver.switch_to.default_content()
    driver.quit()
    

    Result:

    Found: Annual Audited Accounts
    
    Process finished with exit code 0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search