How to click elements in lightbox using python - python

I'm trying to automate an e-commerce website, after i click 'add to cart' a lightbox will be displayed for user to proceed to view cart and to continue to check out.
Here is my sample code, i think my code doesn't find the button because the lightbox is still loading.
driver.find_element_by_id("qty").send_keys("4")
driver.find_element_by_class_name("add_to_cart_btn").click()
Lightbox loading here
driver.find_element_by_class_name("button_primary").click() #for clicking the view cart
driver.find_element_by_link_text("Proceed to Checkout").click()

use the explicit wait method
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.Classname, "add_to_cart_btn")))
element.click();

Related

Unable to click IMDB 'Load More' button using selenium

I am trying to scrape user reviews from the IMDB website using selenium. To load more reviews, I instantiated a click event which clicks the "load more" button on the review page . The issue is that the button gets clicked on the review pages of some movies but it does not work on review pages of other movies.
To give an example, the code given below works when the button is to be clicked on the "Iron Man" user review webpage but on the webpage of "Armageddon", it gives the error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ipl-load-more__button"}
I find this a bit odd because the web element is the same and so if it works on one page, it should work for all the pages. However, I am not able to resolve this issue.
Here is the code which instantiates the click event:
def extend_page(wd): # wd is the webdriver
count = 20
while(count>0):
button = wd.find_element_by_class_name('ipl-load-more__button')
button.click()
wd.execute_script('window.scrollTo(0,document.body.scrollHeight);')
time.sleep(3)
new_height = wd.execute_script('return document.body.scrollHeight')
prev_height = new_height
count-=1
page = wd.page_source
return page
I have tried different methods for instantiating the click event such as find_element_by_name and find_element_by_xpath. I also tried both implicit and explicit wait to give some time for the webpage to load but to no avail.
Can anyone please tell me what could be the reason for this?

How to perform data fetch on button click on a built in html page using selenium

I am new to Selenium and I am trying to mimic user actions on a site to fetch data from a built in html page on button click. I am able to populate all the field details, but button click is not working, it looks like js code not running.
I tried many options like adding wait time, Action chain etc but it didnt work, i am providing site and code i have written.
driver = webdriver.Chrome()
driver.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm")
driver.implicitly_wait(10)
assigned values to all the other fields
driver.find_element_by_id('rdDateToDate').click()
Dfrom = driver.find_element_by_id('fromDate')
Dfrom.send_keys("02-Oct-2020")
Dto = driver.find_element_by_id('toDate')
Dto.send_keys("08-Oct-2020")
innerHTML = driver.execute_script("document.ready")
sleep(5)
getdata_btn = driver.find_element_by_id('getButton')
ActionChains(driver).move_to_element(getdata_btn).click().click().perform()
I recommend using a full xpath.
chrome.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm")
time.sleep(2)
print("click")
fullxpath = "/html/body/div[2]/div[3]/div[2]/div[1]/div[3]/div/div[1]/div/form/div[19]/p[2]/input"
chrome.find_element_by_xpath(fullxpath).click()
I have tried the button clicking and it worked with XPath ... I though its because of someone used the ID twice on a website, but I can not find it ... so i have no idea whats going wrong there ...
Good luck :)

Python selenium can't click a button execute script

I am trying to click a button from the Linkedin public page feed
https://www.linkedin.com/company/bbc-news/
and the button is a post's toolbar button with 3 dots
here is the sample screenshot
and my code is
self.browser.execute_script("document.getElementsByClassName('feed-shared-control-menu__trigger artdeco-button artdeco-button--tertiary artdeco-button--muted artdeco-button--1 artdeco-button--circle artdeco-dropdown__trigger artdeco-dropdown__trigger--placement-bottom ember-view')[1].scrollIntoView();")
self.browser.execute_script("document.getElementsByClassName('feed-shared-control-menu__trigger artdeco-button artdeco-button--tertiary artdeco-button--muted artdeco-button--1 artdeco-button--circle artdeco-dropdown__trigger artdeco-dropdown__trigger--placement-bottom ember-view')[1].click();")
it doesn't return any errors
please kindly help me solve this issue
Thanks in advance for any help
Note: I have already tried webDriverwait , find_elements_by_class_name
The element has an id associated to it, so you can use that id to get the xpath of the element and then you can click it. Also, it is recommended to use the selenium click instead of the javascript executor click(which you are currently using).
Your code should be like:
self.browser.find_element_by_xpath("//button[#id='ember160']//li-icon").click()
If you want to use javascript executor click, then your code should be like:
element = self.browser.find_element_by_xpath("//button[#id='ember160']//li-icon")
driver.execute_script("arguments[0].click();", element)

Prevent/delay Modal Dialog Box from closing using Selenium in Python

I am trying to retrieve the page name of each page on a site. The steps are the following:
Click on the settings icon
a Modal Dialog Box appears (That is what I believe they are called?). The Dialog Box appears for three seconds before closing.
Click on "Rename Page"
A Popup- window appears with a text field (containing the page name). The popup also contains the buttons "Ok" and "Cancel
Retrieve the name in the text field
Press Ok
Go to the next page. Repeat stages 1-4
For some reason at stage 2, seemingly at random, I receive a TimeOutException error. I say at random, because the error occurs on sometimes on say, the 55th page and sometimes 130th page and so on. I believe it might be because the dialog box closes, before the Rename Page button has been clicked (Which is weird since 3 seconds should be ample time to click on the button.
Screenshot
I am using the framework Spyder to write code in. When the TimeOutException occurs, if I run the code from Step 1. Everything works again without error. So for some reason, sometimes the error occurs and sometimes not.
The HTML code for the Settings Icon is the following:
<div
id="_labsfluxbar_WAR_labsfluxbarportlet_showCurrentPageSettingsPopupButton"
class="labs-fluxbar-portlet-current-page-settings-popup">
</div>
The HTML code for 'Rename Page'
<ul
class="labs-fluxbar-portlet-page-menu-items">
<li class="labs-fluxbar-portlet-page-menu-item labs-fluxbar-portlet-page-menu-item-rename" title="Rename this page"
onclick="_labsfluxbar_WAR_labsfluxbarportlet_renamePage('9398653')"> Rename page
</li>
The code in Python:
i = 0
while True:
#Get page name
# Step 1 - Click on Settings icon
settings = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "_labsfluxbar_WAR_labsfluxbarportlet_showCurrentPageSettingsPopupButton"))) #Search for settings button
settings.click() #click settings icon
# Step 2 - Click on the "Rename Page" button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='labs-fluxbar-portlet-current-page-settings-popup-content']//li[#title='Rename this page']"))).click() #Click on the Rename page element
# Step 3 - Retrieve page name from text field
page = driver.find_element_by_xpath("//input[#id='popup_prompt']")
page_name = page.get_property("value") #Copy the name of page
print(page_name)
# Step 4 - Click ok to close PopUp window
driver.find_element_by_xpath("//input[#id='popup_ok']").click() #Exit the popup window
# Step 5- Go to the next page
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "_labsfluxbar_WAR_labsfluxbarportlet_pageDropdownList")))
select = Select(driver.find_element_by_id('_labsfluxbar_WAR_labsfluxbarportlet_pageDropdownList'))
select.select_by_index(i)
i += 1
The TimeOutExcpetion (sometimes) occurs after running the following line, however I don't get why?
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='labs-fluxbar-portlet-current-page-settings-popup-content']//li[#title='Ange ett nytt namn för sidan']"))).click()
Does anyone have any suggestion what is occurring, or what could be done to prevent it?
Embarrassingly the issue was not due to the code.. It was because of a bad/slow internet connection.

Element is not clickable (the button is blocking by other element)

I'm trying to click on this button: browser.find_element_by_id('btnSearch')
But this button is blocking by this div tag: <div id="actionSearch" class="row pull-right">
How do I go around to click this button with id='btnSearch" while it's blocking by the actionSearch div?
I tried the following:
browser.find_element_by_id('btnSearch').click()
browser.implicitly_wait(10)
el = browser.find_element_by_xpath('//*[#id="btnSearch"]')
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
element = browser.find_element_by_id('btnSearch')
browser.execute_script("arguments[0].click();", element)
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, //*[#id="btnSearch"]'))).click()
none of these work.
Can anyone help me with this? I've spent two days trying to click this button!! Please help!
Considering provided image of HTML source (tip: do not provide it as image, but as text instead) I can assume that required element located in the bottom of page and you might need to scroll page down to be able to handle it.
Try below code
link = browser.find_element_by_id("btnSearch")
browser.execute_script("arguments[0].scrollIntoView()", link)
link.click()
Note that link is not a pseudo-element (is not located inside ::before/::after pseudo-element), so it can not be the cause of your problem
As for your code:
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
Here you're trying to make scroll to link with huge offset and then you make click on current mouse position - not on link
You can try to modify it as
ActionChains(browser).move_to_element(el)
ActionChains(browser).click(el) # Pass WebElement you want to click as argument to `click()`
ActionChains(browser).perform()

Categories

Resources