0

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()
7
  • 1
    link inside the href is constantly changing but the xpath of this href is always the same: What do you mean by that? Commented Jan 19, 2022 at 17:16
  • When I copy the href xpath it's always the same but every time I open the page it's a different link Commented Jan 19, 2022 at 17:26
  • @Prophet I tried but I get this error: Unable to locate element: {"method":"xpath","selector":"/html/body/div/p[2]/a"} Commented Jan 19, 2022 at 17:30
  • 1) possibly you are missing a delay 2) can you share all your code and if possible including the link to the page you are trying to work on? Commented Jan 19, 2022 at 17:31
  • Why your code starts from this driver.switch_to.window(driver.window_handles[0]) ? We don't know what you've written before this. Commented Jan 19, 2022 at 19:08

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.