Having trouble scrolling down to element in Selenium, python - python

I'm trying to get Selenium to scroll down on a page and then click on a button (the arrows on the bottom right under the "Add+") , but I keep getting a NoSuchElementException error. Here's what I've tried, but still nothing is working.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
element = driver.find_element_by_xpath('/html/body/div[7]/div/div/main/section[6]/main/div/main/div[2]/section[1]/p/button')
element.click()
Any help is appreciated.

You are using
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
which is basically, to scroll to the bottom of the page.
Please use this :
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
this will basically scroll into the element view and element will be available in Selenium view port.

Related

Scroll to Bottom of Page Not Working (Python Selenium)

Why doesn't driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") work on this apple music playlist? That line of code works fine on every other website I visit.
I tried doing action.send_keys_to_element(driver.find_element(By.TAG_NAME, 'html'), Keys.END) to no avail.
How else could I scroll to the bottom of the page?
Try to use any interactable node like below:
driver.find_element(By.CLASS_NAME, 'play-button').send_keys(Keys.END)

Selenium - Element not interactable

Needing help on this problem. All solutions/suggestions I have researched do not fix it.
Trying to click a button with Selenium. Keep getting these errors.
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: [object HTMLButtonElement] has no size and location
and selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I am not understanding why this element is not interactable.
play_button = driver.find_element_by_xpath('//*[#id="ppp"]/div/div[2]/div[2]/div[2]/button')
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(play_button).click(play_button).perform()
When printing play_button = <selenium.webdriver.remote.webelement.WebElement (session="ca50b37ee2e4b194b2ad5305e254079f", element="78e35dc3-fef8-4082-a6e4-0a92dfbf2ec6")>
I have tried WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="ppp"]/div/div[2]/div[2]/div[2]/button'))) but this times out. Element never becomes clickable.
I have tried using the full xpath.
I have tried disabling extensions/maximize window driver options.
The element is not in a frame or anything. It lies in a div that lies in the body.
The element is clickable within Chrome's console.
Any information/suggestions would be helpful.
Thanks.
The button you are trying to click is hidden, not clickable even with JavaScript executor.
You can start video on that page clicking on
driver.find_element_by_css_selector('div.player-poster.clickable').click()
or
driver.find_element_by_css_selector('div.play-wrapper').click()
No need to use ActionChains(driver).move_to_element etc.
Just a simple click.
Just don't forget setting
driver.implicitly_wait(10)
Or using an explicitly wait of expected conditions to make page loaded before accessing the element.
Using full XPath solved this problem for me.

Handle exception of Pop Up displayed during a Selenium-Python script

I was coding a script written in Py and Selenium that slides some slide and clicks a button of each slide, but sometimes, when the button is clicked, a popup is displayed, and other times the script should go regularly.
Can anybody explain me how to handle the ‘exception’ and How can write correctly the Code?
I'm sure to use Try and Exception, but I don't know how to recognize the Popup, so every attempt I tried failed.
try:
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “x”)))
element.click() #Click Slider Button
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “y”)))
element.click() #Click arrow to go to next slide
exception that, if popup is displayed, do this code
except driver.find_element_by_class_name(“z”).is_displayed():
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “f”)))#click to close popup
element.click()
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, “y”)))
element.click() #click arrow to go to next slide
Try using some popups blocker extensions. If you use a chrome based navigator than this extension works perfectly and will block all possible popups.
Download the extension with this service and follow this question if you don't know how to use extensions with selenium.
This is not exactly the solution you want but it works.

Selenium PhantomJS Scrolling Down

I tried to scrolling down with selenium, But i use webdriver PhantomJS. I tried selenium for test javascript instagram. As you know, on instagram there have button "Load More", after click button "Load More", we don't have to click again becouse it will auto refresh and it will show more image.
I tried like this :
driver.find_element_by_xpath("//a[#class='_8ioip _glw1t']").click()
time.sleep(5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3.6)
Actually this code it's work, but not really work, i mean something weird happening at the "time.sleep". If i give 2, the result are few and if i give 3 the result are just pretty much, But if i give 5, the result same like i give 2.
The question is, How to make teh Scroll get all data
class of "Load more" button seems to have changed.
your code is correct, try with this xpath:
//a[#class='_8imhp _glz1g']
or try to locate the button by it's text content:
//*/div/a[text()[contains(.,'Load more')]]
and before click the button scroll down.. so your code should looks like:
driver.get('https://www.instagram.com/explore/tags/whatever/')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.find_element_by_xpath("//*[text()[contains(.,'Load more')]]").click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

Locate "file upload" button with Selenium

I have the button shown below (image and HTML) and am trying to click it.
Selenium is unable to locate it - I have tried locating by both xpath and by ID.
<input id="wsUpload1" type="file" name="file">
XPATH:
element = driver.find_element_by_xpath('//input[#id="wsUpload1"]')
element.click()
Where am I going wrong?
EDIT: Here is the exception thrown by Selenium:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id="wsUpload1"]"}
Possibilities
Duplicate web element with same id in the page.
Element may be in frame. You need to switch to frame
Trying to access the web element before page is loading.Give some wait time.
Not sure why your button wasn't found, maybe it's because of quotes (although it should show you error in that case), try with driver.find_element_by_xpath(".//input[#id='wsUpload1']") and see if it works. I'm not sure is your button already rendered on the page or you trigger it somehow so it's not there yet?
NoSuchElementException is thrown because your targeted element couldn't be found on that page, it could be that you are on the wrong page, element is not rendered yet so you should wait for it to appear, element could be in some iframe etc etc, it's hard to say when I don't know how your page works.
But if you are trying to upload something you should perform sendKeys() on that button (with path of file which are you trying to upload), not click() on it. This is how selenium upload works.
I have solved it - the driver opens a tab on a side panel and the button is in the tab. There seems to be a few ms delay between clicking the tab and the button appearing so I added a wait until element is clickable and that seems to work.
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='wsUpload1']"))).click()

Categories