0

why doesn't the applied button (button.click()) work in the code?

import time
import undetected_chromedriver as uc

def press_button():
    driver = uc.ChromeOptions()
    driver.headless=True
    driver.add_argument('--headless')
    driver = uc.Chrome(options=driver)
    driver.get('https://kad.arbitr.ru/')
    time.sleep(5)
    try:
        inn_field = driver.find_element("xpath", '//*[@id="sug-participants"]/div/textarea')
        inn_field.send_keys('772000581641')
        button = driver.find_element("xpath", '//*[@type="submit"]')
        button.click()
        time.sleep(5)

    except:
        driver.close()
        driver.quit()


if __name__ == '__main__':
    press_button()

at the same time, the inn_field field is filled in correctly. Also, if I'm not doing all this in headless mode, then everything goes fine ... dom data of html page for submit button

Prophet
  • 32,350
  • 22
  • 54
  • 79
m_sasha
  • 239
  • 1
  • 7

1 Answers1

0

In case this code works in regular mode but doesn't work in headless more probably the issue is that you didn't define screen size for headless mode.
The default screen size in headless mode is much smaller than the default screen size in regular mode.
So, try adding this line options.add_argument("window-size=1920,1080").
Also, it is much better to use WebDriverWait expected_conditions explicit waits than hardcoded sleeps.
And finally //*[@type="submit"] is not a unique locator for that element but it still will work since the button you trying to click is the first match for this XPath. But still, you should always use unique locators. This is a unique locator for that button: "//div[@class='b-form-submitters']//*[@type='submit']"

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • I added `driver.add_argument("window-size=1920,1080")`, changed `"//div[@class='b-form-submitters']//*[@type='submit']"` - but still the application doesn't work... i think the site somehow detects the headless mode and deactivates the button.. – m_sasha Nov 28 '22 at 12:09
  • Well, to check that you will have to make a screenshot. – Prophet Nov 28 '22 at 12:18
  • yes i did it... `inn_field.send_keys('772000581641')` this code works (the field is filled), nothing happens next – m_sasha Nov 28 '22 at 12:31
  • Is submit button appears on the screen and it is enabled? – Prophet Nov 28 '22 at 12:34
  • yes...the button appears, but no action occurs ... and if i start without headless mode, then the button is pressed ... – m_sasha Nov 28 '22 at 13:20
  • I can't debug that. But can you put `time.sleep(1)` after `inn_field.send_keys('772000581641')`, before `button = driver.find_element("xpath", '//*[@type="submit"]')` ? – Prophet Nov 28 '22 at 13:25
  • 1
    I have already tried waiting 5 seconds before the button, it does not work... I keep thinking it's antibot protection – m_sasha Nov 28 '22 at 19:02