eymentakak
eymentakak

Reputation: 43

how can i open link in href with selenium python?

The link inside the href is constantly changing but the xpath of this href is always the same. How can I click on the www.confirmationemail.com ?

<div dir="ltr">
<p>exampleTEXT.</p>
<p><a href="www.confirmationemail.com" target="_blank">www.confirmationemail.com</a></p>
<p>exampleTEXT.</p>
<p>exampleTEXT,</p>
<p>exampleTEXT</p>
</div>

This is the page I'm working on:https://www.minuteinbox.com/

The process is as follows: registering on a site with the e-mail received from here and receiving an e-mail, logging in to the e-mail, but I cannot click on the link in its content.

from selenium import webdriver
from time import sleep
import config2 as cf
from selenium.webdriver.support.select import Select
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from asyncio import sleep
import time


driver = webdriver.Chrome("C:\\ChromeDriver\\chromedriver.exe")


url = "https://www.minuteinbox.com/"
url2 = "EXAMPLE.COM"

driver.get(url)

element =     driver.find_element_by_xpath("XPATH").text
print(element)

time.sleep(4)



driver.execute_script("window.open('');")

driver.switch_to.window(driver.window_handles[1])
driver.get(url2)

sec = driver.find_element_by_xpath("XPATH")
sec.click()

devam = driver.find_element_by_xpath("XPATH")
devam.click()

ad = driver.find_element_by_xpath("XPATH")
ad.send_keys("deneme")

soyad = driver.find_element_by_xpath("XPATH")
soyad.send_keys("test")

eMail = driver.find_element_by_css_selector("#user_email")
eMail.send_keys(element)

eMail2 = driver.find_element_by_css_selector("#user_email_confirmation")
eMail2.send_keys(element)

sifre = driver.find_element_by_css_selector("#user_password")
sifre.send_keys("PASS")

sifre2 =     driver.find_element_by_css_selector("#user_password_confirmation")
sifre2.send_keys("PASS")

buton = driver.find_element_by_css_selector("SELECT")
buton.click()

hesapol = driver.find_element_by_css_selector("SELECT")
hesapol.click()
sleep(2)

driver.switch_to.window(driver.window_handles[0])
time.sleep(7)

bas = driver.find_element_by_css_selector("#schranka > tr:nth-child(1)")
bas.click()

time.sleep(1)

time.sleep(1)
SD = driver.switch_to.frame(driver.find_element_by_css_selector("iframe#iframeMail"))
time.sleep(5)       
SD = driver.find_element_by_xpath("//a[contains(@href,'minuteinbox')]").click

driver.switch_to.default_content()

sd = I put this just to be able to write it in the code section

SOLVED

İMPORTS

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

bas = driver.find_element_by_css_selector("#schranka > tr:nth-child(1)")
bas.click()
time.sleep(3)

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,  "iframe[id='iframeMail']")))
print(driver.page_source)


link = driver.find_element_by_xpath("/html/body/div/p[2]/a")
link.click()

Upvotes: 0

Views: 1492

Answers (1)

Prophet
Prophet

Reputation: 33371

As you mentioned, the XPath of that element is constant.
So you can get that element based the constant XPath locator and click it. Something like this:

driver.find_element_by_xpath('the_constant_xpath').click()

UPD
The element you want to be clicked can be located by XPath.
However, it is inside an iframe, so in order to access it you will have to switch to that iframe first.
I have also made your locators better.
So your code could be something like this:

driver.switch_to.window(driver.window_handles[0])
time.sleep(5)

bas = driver.find_element_by_css_selector("td.from")
bas.click()

time.sleep(1)
driver.switch_to.frame(driver.find_element_by_css_selector("iframe#iframeMail"))

driver.find_element_by_xpath("//a[contains(@href,'minuteinbox')]").click

When you finished working inside the iframe you will have to get out to the default content with

driver.switch_to.default_content()

Upvotes: 2

Related Questions