0

enter image description here

I can't seem to find the right path for the button marked above. Any help on how to simulate a click on this button?

This is the website, and linked under is a greater part of the source code.

enter image description here

enter image description here

  • Please read why a [screenshot of HTML or code or error is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. – DebanjanB Aug 19 '20 at 19:34

2 Answers2

0

To click on the element with text as Lukk 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, "ytcp-button#close-button div[label='Lukk']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytcp-button[@id='close-button']//div[@label='Lukk' and text()='Lukk']"))).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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    Just tried it, and it didn't work. I got this error message if it gives something away; "//ytcp-button[@id='close-button']//div[@label='Lukk' and text()='Lukk'][@type='submit' and @onclick]"))).click() File "C:\Users\Bruker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Anders Haarberg Eriksen Aug 19 '20 at 19:43
  • @AndersHaarbergEriksen There was a bug, fixed it now. Let me know the status. – DebanjanB Aug 19 '20 at 19:55
  • I would love to say that it worked, but it didn't unfortunately. If it somewhat helps; it's a page of youtube, and that's the close button. – Anders Haarberg Eriksen Aug 19 '20 at 20:05
  • I updated the post with some more info; hope that might help you find a solution. – Anders Haarberg Eriksen Aug 19 '20 at 20:09
  • @AndersHaarbergEriksen How are you concluding _...it didn't unfortunately..._? – DebanjanB Aug 19 '20 at 21:39
  • 1
    What I meant was that the same error occurred, and no button got pressed. – Anders Haarberg Eriksen Aug 20 '20 at 06:30
  • @AndersHaarbergEriksen IMO, you still didn't mention about the error you were seeing initially. – DebanjanB Aug 20 '20 at 06:34
  • Well, the only error I get is a timeouterror. It seems as though it can't find the element regardless of the ingenious solution you gave me. I'm a bit baffled tbh. – Anders Haarberg Eriksen Aug 20 '20 at 08:54
0

I would say to first wait for the popup to open. Explicit wait can do:

Here is a sample code: https://seleniumbyexamples.github.io/waitvisibility

Your selector would be paper-dialog#dialog

Once the popup is visible you can now wait for the button.

You can use wait to be clickable:

https://seleniumbyexamples.github.io/waitclickable

The id would be close-button

You can also try other locators:

https://seleniumbyexamples.github.io/locator

  • Thanks for the attempt to help, but unfortunately it didn't work. For some reason, it struggles to find the element I'm looking for, and I've got no clue as to why that is. – Anders Haarberg Eriksen Aug 20 '20 at 15:39
  • It might that element you are waiting for did not load. If you are doing headless then you can just take a screenshot of the current page before accessing the element. driver.save_screenshot("screenshot.png") – slackingslacker Aug 21 '20 at 09:16