I try to click on the element with the class name "mat-select-arrow-wrapper.ng-tns-c73-7" on the website below. This causes the error "Unable to locate element" until I click the new browser window once, how can I get around this? The other two boxes to the left of this do not exhibit the problem.
My code currently looks like this:
url = "https://www.pamyra.de/"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)
time.sleep(3)
driver.find_element_by_class_name("mat-select-arrow-wrapper.ng-tns-c73-7").click()
CodePudding user response:
You have css selector .mat-select-arrow-wrapper.ng-tns-c73-7
and you are causing by_class_name
is the reason why you are getting the exception.
Please use below code :
driver.find_element_by_css_selector(".mat-select-arrow-wrapper.ng-tns-c73-7").click()
Full code should look like this :
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(30)
driver.maximize_window()
url = "https://www.pamyra.de/"
driver.get(url)
time.sleep(3)
driver.find_element_by_css_selector(".mat-select-arrow-wrapper.ng-tns-c73-7").click()
Update 1 :
With Explicit waits :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
url = "https://www.pamyra.de/"
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#mat-select-value-5 div"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC