Issue is with accepting cookies, I'm not sure why nothing works, tried By.CLASS, By.NAME
etc. to accept cookies "Sutinku su visais"
must be chosen
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
PATH = Service("C:\\Program Files (x86)\\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.get("https://www.telia.lt/privatiems")
driver.implicitly_wait(5)
link = driver.find_element(By.CLASS_NAME, "btn btn-primary js-cookie-modal-accept")
link.click()
Doesnt matter how long I put pause/wait, all the time same issue:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to
locate element
Highly appreciated if anyone can educate me on how to locate in HTML the buttons I want by different criterias, as from what I tried already could not achieve anything.
HTML in which I'm trying to accept cookies:
<div class="button-group button-group--pull-left" data-gtm-vis-has-fired-2746359_1882="1">
<input class="btn btn-primary js-cookie-modal-accept" type="submit" value="Sutinku su visais" data-gtm-vis-has-fired-2746359_1882="1">
<a class="btn btn-link js-cookie-modal-settings" data-gtm-vis-has-fired-2746359_1882="1">
Slapukų nustatymai
</a>
</div>
CodePudding user response:
Class name does not have support for spaces or multiple spaces in Selenium CLASS_NAME
Please covert this
btn btn-primary js-cookie-modal-accept
into a CSS selcetor
by putting .
and removing spaces.
.btn.btn-primary.js-cookie-modal-accept
So your effective code block would be :
link = driver.find_element(By.CSS_SELECTOTR, ".btn.btn-primary.js-cookie-modal-accept")
link.click()
As requested by OP, this is one way to check the HTML DOM
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.
I would also recommend you to use ExplicitWait driven by WebDriverWait
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.btn-primary.js-cookie-modal-accept"))).click()
except:
print("Could not click")
pass
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC