0

I tried a few websites don't have a problem. "baseURL" tried a lot of time.

driver.find the class element

can't get. Anyone can help?

Click this link below and inside the login page ... which one to use ??

Code trials:

PATH = "D:\chromedriver.exe"
driver = webdriver.Chrome(PATH)



baseURL = "https://www.kingdoms.com/#logout"
driver = get(baseURL)

print(driver.title)

driver.find_element_by_xpath
cruisepandey
  • 28,520
  • 6
  • 20
  • 38

2 Answers2

1

There is an nested iframe in that page, so you have to switch to iframe and again to iframe, and then you can send the keys to email address input field.

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://www.kingdoms.com/#logout")
wait = WebDriverWait(driver, 20)
try:
    wait.until(EC.element_to_be_clickable((By.ID, "cmpbntyestxt"))).click()
except:
    pass
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.mellon-iframe")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='https://mellon-t5.traviangames.com/account/logout']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys('Mario@gmail.com')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("marios password")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='submit']"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • many many thanks.I learned a lot, and the last one login button, why i using this one . it showing me Error driver.find_element_by_link_text("Log in").click() – Mario Hooi Sep 04 '21 at 06:17
  • Exception has occurred: NoSuchElementException Message: no such element: Unable to locate element: {"method":"link text","selector":"Log in"} (Session info: chrome=93.0.4577.63) File "C:\Users\Mario\Desktop\learnpy\travian.py", line 22, in driver.find_element_by_link_text("Log in").click() – Mario Hooi Sep 04 '21 at 06:27
  • because i want to click Login button – Mario Hooi Sep 04 '21 at 06:28
  • any idea send Login key ? – Mario Hooi Sep 04 '21 at 06:33
  • @MarioHooi : Updated above, check it out, you will have to give right credentials to make this to work – cruisepandey Sep 04 '21 at 06:40
  • i add one more line click ENTER __ `wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys(usrPwd)` `WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys(Keys.ENTER)` – Mario Hooi Sep 04 '21 at 07:48
  • hmm.. it would cause you are simulating the enter keys, which is a different way, But I tried my code as well in my local machine and it worked. – cruisepandey Sep 04 '21 at 07:50
  • but i am confuse a bit , some solution is ` (Keys.RETURN) ``(Keys.ENTER)` – Mario Hooi Sep 04 '21 at 07:51
  • Both are to press Enter button `(Keys.RETURN)` or `(Keys.ENTER)` – cruisepandey Sep 04 '21 at 07:51
0

The desired fields are within nested <iframe> elements so you have to:

  • Induce WebDriverWait for the parent frame to be available and switch to it.

  • Induce WebDriverWait for the child frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get('https://www.kingdoms.com/#logout')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cmpboxbtn.cmpboxbtnyes]"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.mellon-iframe")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://mellon-t5.traviangames.com/account/logout/applicationDomain']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("Mario@Hooi.com")
      driver.find_element_by_css_selector("input[name='password']").send_keys("MarioHooi")
      
    • Using XPATH:

      driver.get('https://www.kingdoms.com/#logout')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cmpboxbtn cmpboxbtnyes']"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='mellon-iframe']")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://mellon-t5.traviangames.com/account/logout/applicationDomain')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='email']"))).send_keys("Mario@Hooi.com")
      driver.find_element_by_xpath("//input[@name='password']").send_keys("MarioHooi")
      
  • 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
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i tried this 2 code..it showing ERROR Using CSS_SELECTOR: Exception has occurred: TimeoutException Message: File "C:\Users\Mario\Desktop\learnpy\travian.py", line 24, in WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cmpboxbtn.cmpboxbtnyes]"))).click() Using XPATH: Exception has occurred: TimeoutException Message: File "C:\Users\Mario\Desktop\learnpy\travian.py", line 32, in WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cmpboxbtn cmpboxbtnyes']"))).click() – Mario Hooi Sep 04 '21 at 06:23