I'm trying to accept cookies with Selenium, but the accept button is not found. I am not familiar with Selenium and I don't know how to debug. For instance, if I try to accept cookies from webrankinfo.com.
This is my code:
driver = webdriver.Chrome("chromedriver")
driver.get("https://www.webrankinfo.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Tout accepter et continuer']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Tout accepter et continuer')]"))).click()
Whatever the selected option (Xpath or CSS), the button is not found. What is the solution?
Moreover, is there any option to debug Xpath directly on my Web Browser like Chrome or Firefox?
CodePudding user response:
I see no any "accept cookies" button appearing on that web page, so I can't help with providing locator for it.
As about debugging the locators with Chrome of Firefox:
You can press F12
on your browser, it will open the Developer Tools.
Then select Elements
tab there.
Then you can locate any element with the arrow in the upper-left corner there.
It will present all the attributes of the selected element.
Control F
on that view will allow you to search elements on that page with XPath or CSS Selector.
See also here or any other resource about locating web elements with Developer Tools.
CodePudding user response:
As Prophet, no cookie alert appears for me, so I can't help you locate the button. Although, I can help you trying to make the cookie alert not appear at all. Some websites will only require cookies for certain browsers or browser versions. This way, changing the user agent of the WebDriver may cause the alert to disappear. Your code would be like this:
options = webdriver.ChromeOptions()
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
options.add_argument('user-agent={0}'.format(user_agent))
driver = webdriver.Chrome("chromedriver")
driver.get("https://www.webrankinfo.com")
That user agent is just an example, maybe it won't work but others will. You're able to find older versions of Chrome here.
What may be happening, also, is that the website is detecting an automate tool. It would explain why the cookie alert doesn't show up for us. To prevent the detection, you'll have to add some option arguments to WebDriver:
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')
options.add_argument('-allow-insecure-localhost')
options.add_argument('--remote-debugging-port=9222')
options.add_argument('--disable-gpu')
options.add_argument('--start-maximized')
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches",["enable-automation"])
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
options.add_argument('user-agent={0}'.format(user_agent))
driver = webdriver.Chrome("chromedriver")
driver.get("https://www.webrankinfo.com")
Some of these arguments may be useless. So you can test them one by one and let on your code only the usefull ones.