In Selenium 4.21, Python
I am trying to make a bot to spread messages among people in chat website. Everything is going well expect trying to click element within iframe.
Here’s the HTML code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time
chrome_options = Options()
# chrome_options.add_argument(r"--user-data-dir=C:UsersDellAppDataLocalGoogleChromeUser Data") # تحديد مسار ملف تعريف المستخدم
# chrome_options.add_argument("--profile-directory=Profile 1")
# chrome_options.add_argument("--headless") # تفعيل وضع الرأس غير الظاهر
# chrome_options.add_argument("--disable-gpu") # تعطيل GPU لتحسين الأداء في الوضع غير الظاهر
chrome_options.add_argument("--window-size=1920x1080") # تحديد حجم النافذة
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=chrome_options)
#Open
driver.get("https://www.drdchati.com/enter-chat")
username = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "init_user"))
)
username.send_keys("Mohamed")
gender= driver.find_element(By.ID, "init_gender")
select = Select(gender)
# Select an option by visible text
select.select_by_index(2)
login_btn = driver.find_element(By.XPATH, "/html/body/div[6]/div/div[1]/main/article/div/div/form/div[3]/div/button")
login_btn.click()
iframe = WebDriverWait(driver, 45).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[3]/iframe'))
)
driver.switch_to.frame(iframe)
palestine_room = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div[1]/div/div/div[2]/div[2]/div[2]"))
)
palestine_room.click()
time.sleep(30)
But this script doesn’t work!!
It should find the element and go on. but It doesn’t work and the script stopped here and can’t continue.