基本上,我试图让程序找到关键字,如果它设法做到这一点,它将单击页面中 pdf 文件的链接(现在我只想打印一条确认消息)
但是,尽管关键字明显在页面中,selenium 却无法找到它。
这是代码块:
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()
我尝试了各种搜索方法,包括 XPath 和 JavaScript,但我确实无法搜索到它。
我目前正在 PyCharm 上使用 Selenium 仅供参考
因为关键字Annual Audited Accounts是iframe的一部分,selenium无法直接访问,所以需要先切换到iframe
添加以下代码以切换到 ifrma 第一个 driver.switch_to.frame(driver.find_element(By.NAME,'announcement_detail_iframe'))
完整代码
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()
打印
Found: Annual Audited Accounts
检查下面的工作代码:
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:
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")
driver.switch_to.default_content()
driver.quit()
结果:
Found: Annual Audited Accounts
Process finished with exit code 0