0

After a very useful help here to login on trading view, i cannot figure out how to access to the text of this following alert and close it selecting OK Screenshot of an alert in tradingview

here is the html code corresponding to that pop up:

<div class="tv-alert-notification-dialog__head">
        <div class="tv-alert-notification-dialog__icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 51 58" width="51" height="58"><g fill="none"><path fill="#2196F3" d="M29 58c3.3 0 6-3.6 6-8H23c0 4.4 2.67 8 6 8zm16.5-16V28.667c0-8.187-4.51-15.04-12.375-16.854V10c0-2.213-1.843-4-4.125-4s-4.125 1.787-4.125 4v1.813C16.982 13.627 12.5 20.453 12.5 28.667V42L7 47.333V50h44v-2.667L45.5 42z"></path><path stroke="#FFF" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M25.72 15.942c-1.81.416-3.392 1.162-4.73 2.17m-2.213 2.177c-.12.152-.237.308-.35.468"></path><path stroke="currentColor" stroke-width="4" d="M24 54c3.3 0 6-3.6 6-8H18c0 4.4 2.67 8 6 8zm16.5-16V24.667c0-8.187-4.51-15.04-12.375-16.854V6c0-2.213-1.843-4-4.125-4s-4.125 1.787-4.125 4v1.813C11.982 9.627 7.5 16.453 7.5 24.667V38L2 43.333V46h44v-2.667L40.5 38z"></path></g></svg></div>
        <div class="tv-alert-notification-dialog__title">Alert on BTCUSDT</div>
        <div class="tv-alert-notification-dialog__subtitle">BTCUSDT, 1 Crossing Example of alertcondition (close)</div>
    </div>

<div class="tv-dialog__section tv-dialog__section--actions tv-dialog__section--no-border tv-alert-notification-dialog__buttons">
   <div data-name="editAlert" class="js-dialog__action-click js-dialog__no-drag tv-button tv-button--link tv-button--no-padding i-float_left">
     Edit
   </div>
   <div data-name="ok" class="js-dialog__action-click js-dialog__no-drag tv-button tv-button--primary">
     OK
   </div>
</div>

and here are the attempt i tried to get the elements that could give me the text and the OK button to click

elementtext=browser.find_elements_by_xpath("//*[@class='tv-alert-notification-dialog__subtitle']")
browser.find_elements_by_xpath("//*[@class='js-dialog__action-click js-dialog__no-drag tv-button tv-button--primary']").click()

The elementtext returns well an element from which i don't know what to do with and the second commande fails (it is not clickable) thanks for your help and sorry if my level is really low.

1 Answer 1

2

These seems to be fundamental WebDriver Python questions, maybe you should think about following an online course first. I like the trail and error approach, but as you can see your questions get downvoted, because they are so basic.

Be sure to check the documentation of the WebDriver Python API: https://selenium.dev/selenium/docs/api/py/index.html

Possible answers to your questions start here:

To get the text from an element add .text:

element = browser.find_element_by_xpath("//div[@class='yourclass']")
text = element.text

find_elements returns a list of elements, you need to click on one of the items not the list, example for working with lists:

list_of_buttons = browser.find_elements_by_xpath("//div[@class='yourclass']")
for button in list_of_buttons:
  button.click()

Probably you should just loose the s, (and maybe use a shorter XPath):

driver.find_element_by_xpath("//div[contains(text(), 'OK')]").click()

If you have multiple elements with the same selector you could try to find the first that is_displayed() (in case some are hidden):

list_of_buttons = browser.find_elements_by_xpath("//div[@class='yourclass']")
for button in list_of_buttons:
  if button.is_displayed():
    button.click()
  else:
    # not visible do nothing
3
  • Great answer, Niels. +1
    – Brian
    Commented Nov 25, 2019 at 19:35
  • Dear Niels, thanks again for your help. Yes you are right my goal is to study more, but i wasn't sure it was the right tool for what i wanted to do. now i will learn more. but your help has been precious to me for having the hands on... the reading is much easier when i explore on the side. in the documentation i didn't understand the difference between find_elements and find_element, now it is clear. as well as the use of //div to search and also to use contains(text()) to make things easier. thank you very much... all you wrote here is working. awesome.
    – PhilippeGe
    Commented Nov 26, 2019 at 1:37
  • @NielsvanReijmersdal +2
    – PhilippeGe
    Commented Nov 26, 2019 at 3:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.