Reputation: 65
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.keys import Keys
import configparser
from selenium.webdriver import ActionChains
import time
import os
class SeleniumConfig():
def __init__(self):
config = configparser.ConfigParser()
self.absolute = "C:\\Program Files (x86)\\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
self.driver = webdriver.Chrome(self.absolute, options=options)
def jupiter_1(self):
self.driver.get('http://jupiter.cloud.planittesting.com')
self.driver.find_element_by_id("nav-contact").click()
time.sleep(5)
form = self.driver.find_element_by_class_name(
"btn-contact btn btn-primary") # my issue seems to start at the submit button
time.sleep(5)
self.driver.quit()
I'm not sure why i'm not able to use the class name, as the inspection says it is
My error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn-contact btn btn-primary"}
(Session info: chrome=87.0.4280.141)
Upvotes: 0
Views: 114
Reputation: 978
Please use following relative xpath to click submit buttton
//a[@class='btn-contact btn btn-primary']
Upvotes: 0
Reputation: 9969
self.driver.find_element_by_css_selector(".btn-contact.btn.btn-primary")
Spaces in class names need to be replaced with . in order for proper xpathing.
Also try not to use time.sleep() and use the following to allow the page to load and for elements to be clickable.
wait = WebDriverWait(self.driver,10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".btn-contact.btn.btn-primary"))).click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 1
Reputation: 86
In my experience with selenium, the find_element_by_
can be a bit wonky and you sometimes have to try several reference techniques before one finally works. I'd recommend using the full Xpath as that seems to reliably work (it's drawback is that it is not as obvious to read in the code itself). You can get the xpath by inspecting the element in the html dev tool and just right click to copy it. An option for "copy full xpath" should show up somewhere.
With that in mind, you can try your code line to the following and see if it helps:
form = driver.find_element_by_xpath("/html/body/div[2]/div/form/div/a")
Hopefully that can at least point you in the right direction!
Upvotes: 1
Reputation: 65
Nevermind i found the answer or a workaround =>
form = self.driver.find_element_by_link_text( "Submit")
I use find_element_by_link_text to submit and it works.
Upvotes: 0