Getting data after page update using Selenium - python

I am wondering if anyone knows a way to get page data after it updates content from a post request via Selenium. I understand that the way Selenium works is by when you get() a website it parses the content at its current state and then that is what you have have to work with but I am wondering if anyone knows how to get this updated conent without "getting" the page again?

You need to wait for the visibility of some static element after the page is completely updated, before you attempt to get the data inducing WebDriverWait for the visibility_of_element_located() as follows:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//xpath_of_static_element")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Python Selenium ID Tags change each time i refresh the page

I am currently trying to automate a data entry process. Every time I refresh the page the ID tags change and make it impossible. I read that I can use CSS selectors or a possible xpath using tags that don't change. It seems that only the ID tags are the problem.
Below is the HTML code for the button. Every time i try and use the CSS selector i got a no such element exception. I think I am doing it wrong. Please help.
<button type="button" class="dark button secondary" data-automation-id=
"btn-footer-save" data-dojo-attach-event="onclick:saveAndStayPressed"
data-qbo-bind="visible:shouldShowSaveAndStayButton" style>Save</button>
Try this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = new WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Save')]"))).click()
The element gets wrapped in a WebDriverWait until it is interactable, in this case clickable

Clicking Apply LinkedIn button using selenium

I am trying to click on the Apply button on linkedIn but, having a hard time in doing so. I tried to target the id but I noticed that the id always changes when the page reloads.
I've tried:
browser.find_element_by_xpath("//span[text() = 'Apply']").click() and browser.find_element_by_xpath("//button[#type = 'Apply']").click() but it doesn't work.
Use either of the xpath.
browser.find_element_by_xpath("//span[normalize-space(.)='Apply']").click()
OR
browser.find_element_by_xpath("//span[contains(.,'Apply')]").click()
UPADTE:
It seems synchronization issue induce WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH,"//span[normalize-space(.)='Apply']"))).click()
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

xpath for first post on instagram profile not working (python, selenium, chromedriver)

I am trying to click on the first post after navigating to any Instagram profile. I looked at the xpath of the first post of multiple Instagram user's profiles and they all seem to be the same. Here is an example of messi's profile.
Here is my attempt with using chromedriver with python to click on Messi's first post. I have already navigated to https://www.instagram.com/leomessi/, which is Messi's profile.
first_post_elem_click = driver.find_element_by_path('//*[#id="react-root"]/section/main/div/div[4]/article/div[1]/div/div[1]/div[1]/a/div').click()
However, the first post is not being clicked on. Would greatly appreciate any help.
Please check below solution,
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Browser = webdriver.Chrome(executable_path=r"chromedriver.exe")
Browser.get("https://www.instagram.com/leomessi/")
WebDriverWait(Browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//body//div[contains(#class,'_2z6nI')]//div//div//div[1]//div[1]//a[1]//div[1]//div[2]"))).click()
Instead of using the absolute xpath, you should be using relative xpath.
You can click on the first post using the below command(Have applied Explicit wait as well):
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//div[#class='Nnq7C weEfm']//img)[1]"))).click()
You need to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I have just checked this in Firefox: $x('//*[#id="react-root"]/section/main/div/descendant::article/descendant::a[1]'). That should give you what you want, I think.

How to click a button on a website by finding its id

When I run the code, the website loads up fine but then it won't click on the button- an error appears saying the element is not interacterble. What do I need to do to click the button? I am relatively new to this and would be grateful for any help.
I have already tried finding it by id and tag.
page = driver.get("https://kenpreston.co.uk/author/")
element = driver.find_element_by_id('mk-button-31')
element.click()
SOLVED:
I used driver.find_element_by_link_text and this worked fine.
I have checked the website and noticed that mk-button-31 is an id for a div tag and inside it there is an a tag. Try getting the url from the a tag and do another driver.get instead of clicking on it.
Also the whole div tag is not clickable so that is why you are getting this error.
Use sleep from time library to be sure page fully loaded
from time import sleep
page = driver.get("https://kenpreston.co.uk/author/")
sleep(2)
element = driver.find_element_by_id('mk-button-31')
element.click()
Looks like your element is not clickable you need to replace this id selector with the css and need to wait for the element before click on it.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
page = driver.get("https://kenpreston.co.uk/author/")
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#mk-button-31 span"))
element.click();
Consider adding Explicit Wait to your script as it might be the case the DOM had finished loading and the button you're looking for is still not there.
The classes you're looking for are:
WebDriverWait
expected_conditions
Suggested code change:
#your other imports here
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
#your other code here
page = driver.get("https://kenpreston.co.uk/author/")
element = WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.ID, "mk-button-31")))
element.click()
More information: How to use Selenium to test web applications using AJAX technology

How to find element by xpath in Selenium Python?

I am trying to use Selenium in Python to click on a link to a report on a web page. I have it working up to the point where it opens the page that the report is on, but I am having trouble actually clicking that specific report.
The page has a list of reports all with the same class. This is what I get when I inspect that specific report for example:
<a class="rpt" href="reportConfigRedirect.asp?action=filter&rc_id=181786&letter=">Run with new filters</a>
I have tried:
driver.find_element_by_xpath("xpath")
and this doesn't seem to work, it doesn't do anything once it gets to that page with the report.
Induce WebdriverWait and element_to_be_clickable.Use the following Xpath.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[#class='rpt'][contains(.,'Run with new filters')]")))
element.click()
If unable to click using Webdriver try use javascript executor.
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[#class='rpt'][contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)
EDITED
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)
OR
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(#hef,'reportConfigRedirect.asp?action=filter')][contains(.,'Run with new filters')]")))
driver.execute_script("arguments[0].click();",element)

Categories