Home > Enterprise >  How to make nonclickable button click able in python using selenium?
How to make nonclickable button click able in python using selenium?

Time:03-02

Hi Before starting Thanks for the help in advance So I am trying to scrape google flight website : Output Error

and the code is

from time import sleep
from selenium import webdriver
chromedriver_path = 'E:/chromedriver.exe'
def search(urrl):
 driver = webdriver.Chrome(executable_path=chromedriver_path)
 driver.get(urrl)
 asd= "//div[@aria-label='Enter your destination']//div//input[@aria-label='Where else?']"
 driver.find_element_by_xpath("/html/body/c-wiz[2]/div/div[2]/c-wiz/div/c-wiz/c-wiz/div[2]/div[1]/div[1]/div[1]/div[2]/div[1]/div[4]/div/div/div[1]/div/div/input").click()
 sleep(2)
 TextBox = driver.find_element_by_xpath(asd)
 sleep(2)
 TextBox.click()
 sleep(2)
 print(str(TextBox))
 TextBox.send_keys('Karachi')
 sleep(2)
 search_button = driver.find_element_by_xpath('//*[@id="yDmH0d"]/c-wiz[2]/div/div[2]/c-wiz/div/c-wiz/c-wiz/div[2]/div[1]/div[1]/div[2]/div/button/div[1]')
 sleep(2)
 search_button.click()
 print(str(search_button))
 sleep(15) 
 print("DONE")
 driver.close()
def main():
 url = "https://www.google.com/travel/flights"
 print(" Getitng Data ")
 search(url)
if __name__ == "__main__":
 main()

and i have done it by copying the Xpath using dev tools Thanks again

CodePudding user response:

Instead of this absolute xapth

//*[@id="yDmH0d"]/c-wiz[2]/div/div[2]/c-wiz/div/c-wiz/c-wiz/div[2]/div[1]/div[1]/div[2]/div/button/div[1]

I would recommend you to have a relative path:

//button[contains(.,'Search')]

Also, I would recommend you to have explicit wait when you are trying a click operation.

Code:

search_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Search')]")))
search_button.click()

You'd need below imports as well:

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

Pro tip:

Launch the browser in full screen mode.

driver.maximize_window()

You should have the above line just before driver.get(urrl) command.

CodePudding user response:

The problem you are facing is that after entering the city Karachi in the text box, there is a suggestion dropdown that is displayed over the Search Button. That is the cause of the exception as the dropdown would receive the click instead of the Search button. The intended usage of the website is to select the city from the dropdown and then continue.

A quick fix would be to first look for all of the dropdowns in the source (there are a few) and look for the one that is currently active using is_displayed(). Next you would select the first element in the dropdown suggested:


.....
TextBox.send_keys('Karachi')
sleep(2)

# the attribute(role) in the dropdowns element are named 'listbox'. Warning: This could change in the future
all_dropdowns = driver.find_elements_by_css_selector('ul[role="listbox"]')

# the active dropdown
active_dropdown = [i for i in all_dropdowns if i.is_displayed()][0]

# click the first suggestion in the dropdown (Note: this is very specific to your search. It could not be desirable in other circumstances and the logic can be modified accordingly)
active_dropdown.find_element_by_tag_name('li').click()

# I recommend using the advise @cruisepandey has offered above regarding usage of relative path instead of absolute xpath
search_button = driver.find_element_by_xpath("//button[contains(.,'Search')]")
sleep(2)
search_button.click()
.....

Also suggest to head the advise provided by @cruisepandey including research more about Explicit Waits in selenium to write better performing selenium programs. All the best

  • Related