On python 3.9 and selenium 4.0.0
So I'm currently trying to go to a website and check if there is text there like "Please enter your username" and if that text is not there, it'll open up a different tab and go to a different website.
This is my current code:
driver = webdriver.Chrome(ChromeDriverManager().install()) # opens a new chrome window
driver.maximize_window() # maximizes the window
driver.get('website.com')
if driver.find_element(By.XPATH, '//*[@id="outside"]/div[4]/div/p [contains(text(), "Please enter your Username and Password to continue.")]').click() is True:
loginBox = driver.find_element(By.XPATH, '//input[@id="username"]')
loginBox.send_keys("username")
password = driver.find_element(By.XPATH, '//input[@type = "password"]')
password.send_keys("password")
else:
pass
driver.execute_script("window.open('about:blank','secondtab');")
driver.switch_to.window("secondtab")
driver.get("secondwebsite.com")
...
It currently just opens the website and sits there while not checking for any of the conditions I wanted.
CodePudding user response:
.click
returns type is void, so it would not make any sense to have it returned as true.
Also, if you use find_elements
(Plural), you would have a list
.
Please use the below code :
try:
if len(driver.find_elements(By.XPATH, "//*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]")) > 0:
print("This means that the above xpath is valid and have a node in HTMLDOM")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]"))).click()
loginBox = driver.find_element(By.XPATH, '//input[@id="username"]')
loginBox.send_keys("username")
password = driver.find_element(By.XPATH, '//input[@type = "password"]')
password.send_keys("password")
else:
print("Please check this xpath, //*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]")
pass
except:
pass
driver.execute_script("window.open('about:blank','secondtab');")
driver.switch_to.window("secondtab")
driver.get("secondwebsite.com")
I have induced Explicit waits (WebDriverWait)
, Please use below imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.
xpath that you should check :
//*[@id='outside']/div[4]/div/p [contains(text(), 'Please enter your Username and Password to continue.')]
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
CodePudding user response:
You are looking for some element containing Please enter your Username and Password to continue.
text immediately after opening that page with driver.get('website.com')
.
This may cause that you checking this element content while it is still not fully rendered.
Also,
driver.find_element(By.XPATH, '//*[@id="outside"]/div[4]/div/p [contains(text(), "Please enter your Username and Password to continue.")]').click()
Doesn't return Boolean, it returns nothing.
So it is never True
That's why you immediately passed to the else
where you doing nothing, just pass