2

enter image description hereI'm trying to get rid of cookies popup by accepting cookies and clicking confirm. I don't have any problem with clicking an input "zgadzam się na", but clicking a button "potwierdź"seems to be impossible. My code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

auction_url='https://www.g2a.com/grand-theft-auto-v-rockstar-key-global-i10000000788017'

driver = webdriver.Chrome()


driver.get(auction_url)

add_popup = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//*[@id="webklipper-publisher-widget-container-notification-frame"]'))
)

driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="webklipper- 
publisher-widget-container-notification-frame"]'))
print('ads popup detected')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="webklipper-publisher-widget- 
container-notification-close-div"]').click()
print('ads popup closed')
driver.switch_to.default_content()

driver.execute_script("document.querySelector('#cookies-select-all').click();")#works fine
time.sleep(3)
driver.execute_script("document.querySelector('body > div:nth-child(76) > div > div > div > div > div.modal-options__buttons > button.btn.btn-primary').click();")#error

The error i get:

selenium.common.exceptions.WebDriverException: Message: javascript error: Cannot read property 'click' of null

Interestingly, when I execute this js code in browsers console, i don't get any errors

3
  • 1
    what the button you want to click if possible screenshot Commented Apr 13, 2019 at 11:32
  • 1
    could you add the html surrounding the button :) Commented Apr 13, 2019 at 11:49
  • I've added a screenshot, html is aviable at g2a.com/grand-theft-auto-v-rockstar-key-global-i10000000788017 Commented Apr 13, 2019 at 11:56

3 Answers 3

4

Try below code work fine with me:

driver.execute_script("document.querySelector('body > div:nth-child(60) > div > div > div > div > div.modal-options__buttons > button.btn.btn-primary > span').click();")

OR you can with the below one for find element

driver.find_element_by_css_selector("div.modal-window button.btn-primary").click()
Sign up to request clarification or add additional context in comments.

Comments

0

I discovered that this website creates crazy amount of <div class="ReactModalPortal"></div>, and I was using div:nth-child(x), where x is this strange, non-constant number of blank divs. My x was constant so I was making wrong selection...

Here's a solution:

driver.execute_script("document.getElementsByClassName('modal-options__buttons')[0].getElementsByTagName('button')[0].click()")

Comments

0

Instead of

driver.execute_script("document.querySelector('body > div:nth-child(76) > div > div > div > div > div.modal-options__buttons > button.btn.btn-primary').click();")#error

Try with button click with CSS Selector.

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button.btn.btn-primary span'))).click()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.