E
E
el-bekasto2021-06-11 19:52:02
Python
el-bekasto, 2021-06-11 19:52:02

Why is Selenium not finding the button?

When a request is made for a discord invite link and it turns out that the link is already outdated, selenium correctly finds the "Go to Discord" button:

button = driver.find_element_by_css_selector("[type='button']")

But when such a link is relevant, it cannot find the "Invitation Accepted" button. In doing so, they look like this:
<button type="button" class="marginTop40-i-78cZ button-3k0cO7 button-38aScr lookFilled-1Gx00P colorBrand-3pXr91 sizeLarge-1vSeWK fullWidth-1orjjo grow-q77ONN"><div class="contents-18-Yxp">Accept Invite</div></button>

<button type="button" class="marginTop40-i-78cZ button-3k0cO7 button-38aScr lookFilled-1Gx00P colorBrand-3pXr91 sizeLarge-1vSeWK fullWidth-1orjjo grow-q77ONN"><div class="contents-18-Yxp">Continue to Discord</div></button>

Codes for "Accept Invitation" and "Go to Discord" respectively. Why doesn't it find the first button if their codes are almost identical? Is there any other way to detect this button in selenium?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2021-06-14
@el-bekasto

standard search approach by button tact - xpath (there are also alternatives, for example, protractor)

text = 'Continue to Discord'
text = 'Принять приглашение'
driver.find_element_by_xpath('//button/div[contains(text(), "{}")]'.format(text))

but if the encoding is not configured, it may not be found
{"method":"xpath","selector":"//button/div[contains(text(), "Принять приглашение")]"}

and then a custom expectation is written using kirtranslit
from cyrtranslit import to_latin
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import WebDriver
def waittest:
  def __init__(self, selector, value):
    self.selector = selector
    self.value = value
  def __call__(self, driver):
    element = driver.find_element_by_css_selector(self.selector)
    text = element.text
    print('checking text: "{}" against "{}"'.format(to_latin(text, 'ru'), self.value))
    if to_latin(text, 'ru') == self.value:
      return element
    else:
      return None

text = to_latin('Принять приглашение', 'ru')  
element = WebDriverWait(driver, 10).until( waittest("button[class *= 'button']", text))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question