There are two pop-ups: One that asks if you live in California and the second one looks like this:
Here is my code:
The second pop-up doesn't show up every time and when it doesn't the function works. When it does I get an element not interactable error and I don't know why. Here is the inspector for the second pop-up close-btn.
test_data = ['Los Angeles','San Ramon']
base_url = "https://www.hunterdouglas.com/locator"
def pop_up_one(base_url):
driver.get(base_url)
try:
submit_btn = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"[aria-label='No, I am not a California resident']")))
submit_btn.click()
time.sleep(5)
url = driver.current_url
submit_btn = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, "//*[contains(#class,'icon')]")))
print(submit_btn.text)
#submit_btn.click()
except Exception as t:
url = driver.current_url
print(t)
return url
else:
url = driver.current_url
print("second pop_up clicked")
return url
I have tried selecting by the aria-label, class_name, xpath, etc. the way I have it now shows that there is a selenium web element when I print just the element but it doesn't let me click it for some reason. Any direction appreciated. Thanks!
There are 41 elements on that page matching the //*[contains(#class,'icon')] XPath locator. At least the first element is not visible and not clickable, so when you trying to click this submit_btn element this gives element not interactable error.
In case this element is not always appearing you should use logic clicking element only in case the element appeared.
With the correct, unique locator you code can be something like this:
submit_btn = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"[aria-label='No, I am not a California resident']")))
submit_btn.click()
time.sleep(5)
url = driver.current_url
submit_btn = driver.find_elements_by_xpath('button[aria-label="Close"]')
if(submit_btn):
submit_btn[0].click()
Here I'm using find_elements_by_xpath, this returns a list of web elements. In case element was not found it will be an empty list, it is interpreted as Boolean False in Python.
In case the element is found we will click on the first element in the returned list which will obviously the desired element.
UPD
See the correct locator used here.
It can also be in XPath syntax as //button[#aria-label="Close"]
Related
In selenium python code i can click to WebElement (e.g post_raw.click())
Can I identify WebElement link (which will be clicked) with help of selenium methods?
I know about driver.current_url but I am lookink for link before click. I was looking in documentation, but don't find solution https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html
My code example:
from selenium import webdriver
# login to facebook code
driver.get("https://touch.facebook.com/dota2")
posts_raw = driver.find_elements_by_xpath("//article")
post_raw = posts_raw[0]
print(type(post_raw)) # <class 'selenium.webdriver.remote.webelement.WebElement'>
post_raw.click() # how can I get post_raw link (which was clicked in this line)
I want function like this:
def get_url_from_WebElement(web_elem: WebElement) -> str:
You can try get href from xpath.
Example :
I want to get link form "question" so i add .get_attribute("href") to find_element
question_link = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div[1]/div[3]/div[3]/h2[1]/a[1]").get_attribute("href")
if we print it we get :
https://stackoverflow.com/questions/71371250/selenium-get-object-url
Let's say I have a webelement and I want to use that webelement to get the a tag. This does not get the element clicked which is just the middle of the web_element generally.
def get_url_from_WebElement(web_element):
try:
elem=web_element.find_element(BY.XPATH,".//a").get_attribute("href")
return elem
except:
return ''
Do you mean:
def get_url_from_webelement(xpath):
the_url = driver.find_element(By.XPATH, "\"" + xpath + "\"").get_attribute("href")
return the_url
I want to open 'text' botton but I can't open it.
How can I do?
from selenium import webdriver
import time
driver = webdriver.Chrome(path)
driver.get("https://www.pdfescape.com/account/unregistered/")
time.sleep(0.5)
elem = driver.find_element_by_id("dStartNew").click()
time.sleep(0.5)
elem = driver.find_element_by_id("PdfNew_input_pc").click()
driver.find_element_by_css_selector("[value=\"2\"]").click()
#time.sleep(0.5)
elem = driver.find_element_by_id("PdfNew_input_ps").click()
driver.find_element_by_css_selector("[value=\"a4\"]").click()
#time.sleep(0.5)
driver.find_element_by_xpath('//*[#id="PdfNew_form"]/input[1]').click()
time.sleep(0.5)
driver.find_elements_by_class_name('Add-Text').click()
Problem code : driver.find_elements_by_class_name('Add-Text').click()
The element you wanted to click has no class name - it has the title "Add Text", also you are using find_elements instead of find_element which changes the result type.
So you could select it with:
driver.find_element_by_css_selector('a[title=\"Add Text\"]').click()
Also please let me add another improvement:
driver.find_element_by_xpath('//*[#id="PdfNew_form"]/input[1]').click()
could be much more stable (for the case of page changes) written as:
driver.find_element_by_css_selector('input[type=\"submit\"]').click()
driver.find_elements_by_class_name('Add-Text'),click()
Above code should return a list of elements and you can not click on list of elements
This element is not present in the DOM
To click we use '.' instead of ','
Currently mu scenario is so:
send send_keys to a field
click in "save" button (refresh the page after click)
get get_attribute("value")after refresh the page
My code:
def fill_twitter(self):
enter_twitter = "http://" + fake.text(max_nb_chars=13) + ".com"
twitter_field = self.find_element(*ContractorsLocators.TWITTER)
twitter_field.send_keys(enter_twitter)
self.find_element(*ContractorsLocators.SAVE_BUTTON).click()
time.sleep(4)
assert twitter_field.get_attribute("value") == enter_twitter
My error:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
How can I get the get_attribute("value")?
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
The most frequent cause of this is that page that the element was part of has been refreshed, or the user has navigated away to another page.
As you are saying page is getting refreshed when click on save button. Hence to avoid this you need to find same element (refresh element) again to getting their value as below :-
assert self.find_element(*ContractorsLocators.TWITTER).get_attribute("value") == enter_twitter
As the page got refreshed, you better save the value of the attribute before the element gets Stale.
def fill_twitter(self):
enter_twitter = "http://" + fake.text(max_nb_chars=13) + ".com"
twitter_field = self.find_element(*ContractorsLocators.TWITTER)
twitter_field.send_keys(enter_twitter)
value = twitter_field.get_attribute("value")
self.find_element(*ContractorsLocators.SAVE_BUTTON).click()
time.sleep(4)
assert value == enter_twitter
To check element is visible after page is refreshed use explicit sleep() function and after 4 seconds check that component is visible or not using WebDriverWait.
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"));
Visit http://selenium-python.readthedocs.io/waits.html#explicit-waits for better understanding.
I have this Selenium Code that should click on a size selection button.
submit_button = driver.find_element_by_class_name('pro_sku')
elementList = submit_button.find_elements_by_tag_name("a")
elementList[3].click()
It works for other pages but now on one page I get this error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (663, 469). Other element would receive the click:
I don't understand it because I can look at the browser window that Selenium opens and I normally can click on these buttons.
How can I solve this?
Someone asked for the website. Here it is: http://de.sinobiological.com/GM-CSF-CSF2-Protein-g-19491.html
You can use Xpath for element selection and then use the following method
# Click on Element
def element_click(self, xpath):
xpath = re.sub('"', "'", xpath)
browser.execute_script("""
var elements = document.evaluate("%s",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
var im = elements.snapshotItem(0);
im.click();
""" %(xpath)
)
So if your x-path is correct and item is present on DOM then definitely it will get clicked.
Happy Coding
You can use action_chains to simulate mouse movment
actions = ActionChains(driver)
actions.move_to_element(elementList[3]).perform()
elementList[3].click()
Edit
The <a> tags are not the actual sizes. Try
sizes = driver.find_elements_by_class_name('size_defaut')
sizes[3].click()
Try below:-
driver.execute_script("arguments[0].click();", elementList[3])
Hope it will help you :)
In both chrome and firefox, everything is fine up until I need to extract text. I get this error:
h3 = next(element for element in h3s if element.is_displayed())
StopIteration
I even added a fluent wait.
browser = webdriver.Firefox()
browser.get('https://www.voilanorbert.com/')
inputElement = browser.find_element_by_id("form-search-name")
inputElement.send_keys(leadslist[i][0])
inputElement = browser.find_element_by_id("form-search-domain")
inputElement.send_keys(leadslist[i][1])
searchbutton = browser.find_element_by_name("search")
searchbutton.click()
wait = WebDriverWait(browser, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
wait2 = WebDriverWait(browser, 3000, poll_frequency=100, ignored_exceptions=[ElementNotVisibleException])
wait2.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "h3.one")))
h3s = browser.find_elements_by_css_selector('h3.one')
h3 = next(element for element in h3s if element.is_displayed())
result = h3.text
I think its because its not actually extracting anything, so its just an empty list.
Some pictures that will probably help:
This is the before picture:
This is the after picture:
I need to extract what is in the "text-center displayed" class of the "result" class.
The answer is fairly simple, you just need a different selector when waiting for the search result.
The approach below (C#) works perfectly, it'll reduce your code with a few lines.
One "result DIV" becomes visible when the search is done. It is the only element with the "text-center displayed" class, so that's all your selector needs.
Once such a DIV is displayed, you know where to pinpoint the H3 element (it's a child of said DIV).
So simply wait for the below element to become visible after you've clicked the search button:
IWebElement headerResult = w.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("div[class=\"text-center displayed\"] h3")));
string result = headerResult.Text;