I have to automate account creation for few "Senior Citizen" that live in nursing home. (for free, I'm volunteer) I'm figuring out how to click on button like this :
<button type="submit" data-uid="id-3" role="button" class="_2CEO5 _2eWEL _3cHWt _2Jtx4 V9Kxm _1s9tX _2eWEL _3cHWt _2eWEL _3cHWt _2Jtx4 _3cHWt _5Qrl3">
<span class="_100nC"><div class="_2EteE _2WXqV _34xiU _1TcN5 _2WXqV _34xiU" style="border-top-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255);"></div></span><span class="_2HwQ3">Create an Account</span></button>
I tried with on chrome browser , righ-click > copy Xpath
driver.find_element_by_xpath('//*[@id="plex"]/div/div/div/div[1]/form/button')
but the Selenium cant find it
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="plex"]/div/div/div/div[1]/form/button"}
How to click ? Thank for any hints
The url is: https://www.plex.tv/sign-up/
CodePudding user response:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://www.plex.tv/sign-up/")
driver.maximize_window()
try:
driver.find_element_by_xpath("/html/body/div[2]/div[3]/a[1]").click()
driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[4]/div/a").click()
driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[5]/a").click()
except Exception as e:
print("Nothing to click before submission!")
iframe_el = driver.find_element(By.ID, "fedauth-iFrame")
# iframe_el.get_attribute("src")
try:
driver.get(iframe_el.get_attribute("src"))
# fill the form
element = (
WebDriverWait(driver, 10)
.until(
EC.presence_of_element_located(
(By.XPATH, "/html/body/div[1]/div/div/div/div[1]/form/button")
)
)
.click()
)
print("button is found")
except Exception as e:
print("no element")
finally:
driver.quit()
CodePudding user response:
Instead of targetting the parent BUTTON target the inner SPAN inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit'][data-uid^='id'] span:nth-of-type(2)"))).click()
Using
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit' and starts-with(@data-uid, 'id')]//span[text()='Create an Account']"))).click()
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
References
You can find a couple of relevant discussions on NoSuchElementException in:
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
CodePudding user response:
Navigating directly to their login auth iframe link
from selenium import webdriver
driver = webdriver.Chrome(executable_path=path)
driver.implicitly_wait(10)
driver.get('https://www.plex.tv/sign-up/')
#accept cookies
driver.find_element_by_xpath('/html/body/div[8]/div/div/div[3]/a[2]').click()
#navigate to iframe src
driver.get(driver.find_element_by_id("fedauth-iFrame").get_attribute('src'))
#input email
driver.find_element_by_xpath('//*[@id="email"]').send_keys("[email protected]")
#input password
driver.find_element_by_xpath('//*[@id="password"]').send_keys('password')
#submit
driver.find_element_by_xpath('//*[@id="plex"]/div/div/div/div[1]/form/button/span[2]').click()