-2

I want to find out the "Accept All" button xpath for click accept cookies.

Code trials:

from ast import Pass
import time
from selenium import webdriver

driver = driver = webdriver.Chrome(executable_path=r'C:\Users\Nahid\Desktop\Python_code\Jobsite\chromedriver.exe')  # Optional argument, if not specified will search path.
driver.get('http://jobsite.co.uk/')
driver.maximize_window()
time.sleep(1)
#find out XPath in div tag but there has another span tag 
cookie = driver.find_element_by_xpath('//div[@class="privacy-prompt-button primary-button ccmgt_accept_button "]/span')
cookie.click()

2 Answers2

1

Your XPath looks correct but if can be improved.
Also you should use WebDriverWait expected conditions instead of hardcoded sleeps.
As following:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("--start-maximized")

s = Service('C:\webdrivers\chromedriver.exe')

driver = webdriver.Chrome(options=options, service=s)

url = 'http://jobsite.co.uk/'

wait = WebDriverWait(driver, 10)
driver.get(url)
wait.until(EC.element_to_be_clickable((By.ID, "ccmgt_explicit_accept"))).click()
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • You could further improve the XPath by not using it at all... `By.ID()` would be better here since all you are using is an ID. – JeffC Sep 03 '22 at 23:48
  • But internally By.ID, By.Classname etc are all converted to CSS Selectors or XPath. NO? – Prophet Sep 04 '22 at 07:13
  • That's really not the point here but yes, they are converted to CSS selectors but for readability By.ID makes more sense in this case. In general, you should prefer By.ID first, and then CSS selectors over XPath, when possible, because they are faster, better supported, and the syntax is simpler. There are some uses for XPath because only it can find elements by contained text and do complex DOM traversal. – JeffC Sep 04 '22 at 13:58
1

The desired element:

<div id="ccmgt_explicit_accept" class="privacy-prompt-button primary-button ccmgt_accept_button ">
    <span>Accept All</span>
</div>

is a <span> tag having an ancestor <div>.


Solution

To click on the clickable element you need to induce 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, "div.privacy-prompt-button.primary-button.ccmgt_accept_button>span"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Accept All']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352