I have the following code;
if united_states_hidden is not None:
print("Country removed successfully")
time.sleep(10)
print("type(united_states_hidden) = ")
print(type(united_states_hidden))
print("united_states_hidden.text = " + united_states_hidden.text)
print("united_states_hidden.id = " + united_states_hidden.id)
print(united_states_hidden.is_displayed())
print(united_states_hidden.is_enabled())
united_states_hidden.click()
The outputs to the console are as follows:
Country removed successfully
type(united_states_hidden) =
<class 'selenium.webdriver.remote.webelement.WebElement'>
united_states_hidden.text = United States
united_states_hidden.id = ccea7858-6a0b-4aa8-afd5-72f75636fa44
True
True
As far as I am aware this should work as it is a clickable web element, however, no click is delivered to the element. Any help would be appreciated as I can't seem to find anything anywhere else. The element I am attempting to click is within a selector box.
Seems like a valid WebElement given you can print all of the info. like you did in your example.
It's possible the element located is not the element that is meant to be clicked, so perhaps the click is succeeding, but not really clicking anything.
You could try using a Javascript click and see if that helps:
driver.execute_script("arguments[0].click();", united_states_hidden)
If this does not work for you, we may need to see the HTML on the page and the locator strategy you are using to find united_states_hidden so that we can proceed.
Related
I have some page and "tree menu". When I want to open tree_menu, I need to click on button. After, I make some steps and click on title, for example, I clicked "19,20", after I clicked "may" and after I clicked "2" and in the top of page (where i choose this tree_menu) I have a button/link. And text in this element with 2 space (not one):
"19,20 > may > 2"
But on display I think I see with 1 space, but in tag value with 2. And this ">" is text, but I don't know how it understands and sees selenium.
So, after I call my function for search this text:
def isText(self, text):
try:
element = self.driver.find_element(By.XPATH, "//*[text()[contains(., \"" + text + "\")]]")
return True
except:
return False
So, but selenium can't to find this text. And it could find only for "19,20", but space or next text - no.
I opened web-code, but I didn't see new tag or something else.
But I tried find this button/link like element and check that text and it worked.
This project work with dojo, but i can't to change anything.
Please, can you help me with this problem?
TY!
Have a good day!
I didn't get exactly what your intension but if you want to find the button and click using selenium then follow the format.
element = self.driver.find_element(By.XPATH," COPY FULL XPATH").clik()
or
element = self.driver.find_element(By.XPATH," COPY FULL XPATH")
time.sleep(5)
element.click()
Just find the tag that contains that text.
element = self.driver.find_element(By.XPATH, "//*[contains(text(), '{}')]".format(text))
I'm trying to change the chrome profile name on this like : chrome://settings/manageProfile using Python and Selenium :
It's the empty textbox on the top left corner:
The issue is that I can't access to the element, I tried all the stuff below :
chromeProfilName = browser.find_element(By.XPATH, "//*[#id='profile-name']")
chromeProfilName = browser.find_element(By.ID, "profile-name")
chromeProfilName = browser.find_element(By.ID, "input")
I don't really understand how the HTML page is made, but when I examine the page, I found the textboxID = "input". However, the value is stored in a span, which ID is "profile-name".
I always have the same issue : "no such element: Unable to locate element". I don't have a deep knowledge about Selenium. I already looked for answers on internet but I found nothing.
Thanks !
To access the input element you need traverse through shadowroot element.Use the following querySelector to indentify the input tag.
driver.get("chrome://settings/manageProfile")
profileInput = driver.execute_script('return document.querySelector("settings-ui").shadowRoot.querySelector("settings-main").shadowRoot.querySelector("settings-basic-page").shadowRoot.querySelector("settings-people-page").shadowRoot.querySelector("settings-manage-profile").shadowRoot.querySelector("cr-input").shadowRoot.querySelector("#input")')
profileInput.click()
profileInput.clear()
profileInput.send_keys("user676767")
Browser snapshot:
The accepted answer works, I just want to add something :
For those who thought you've to search yourself the path with shadowroots, in reality you don't have to.
With Chrome for exemple (it's not possible on Firefox), when you target the element in the source code, copy the "JS path". Then you just have to paste the path in the execute_script function.
My code:
website = browser.find_element_by_link_text('Website')
if website:
website.click()
else:
print('no website')
What I am trying to do is click the button if it is available on the page. If the button isn't available I want it to print no website available on the console and proceed to the next step.
I do not know what I am doing wrong does anyone know how to do fix this?
Thanks in advance I am new to coding!
Before you find an element, you need first visit a website.
driver = webdriver.Firefox()
print('Acessando web site: {}'.format(os.getenv('VISIT_URL')))
driver.get('www.example.com')
#here onwards you can access browser elements like buttons links etc
You are giving a instance of Web element to if() condition, where a boolean expression is expected.
1) Try checking the presence first using find_elements_by_link_text() # _elements_
if len(driver.find_elements_by_link_text('Website')) > 0:
driver.find_element_by_id('Website').click()
2) Or use expected_conditions to check whether the element is available; expected_conditions documentation
3) Or use try/except block;
try:
website = driver.find_elements_by_link_text('Website')
except NoSuchElementException:
# code to execute if the expected element is not available
For the longest time I had an issue with selenium finding a button I was looking for on the page. After a week I got the "brilliant" idea to check the url that selenium was searching the button for. Dummy me, it was the wrong URL.
So the issue is, selenium searches page1 for a specific item. It then clicks it, and by the websites design, it opens page2 in a new tab. How can I get selenium to follow the click and work on the new tab?
I though of using beautiful soup to just copy the url from page1, but the website doesn't show the urls. Instead it shows functions that get the urls. It's really weird and confusing.
Ideas?
all_matches = driver.find_elements_by_xpath("//*[text()[contains(., 'Pink')]]")
item = all_matches[0]
actions.move_to_element(item).perform()
item.click()
try:
print (driver.current_url)
get_button = driver.find_elements_by_xpath('//*[#id="getItem"]')
except:
print 'Cant find button'
else:
actions.move_to_element(get_button).perform()
get_button.click()
Selenium treats tabs like windows, so generally, switching to new window/tab is as easy as:
driver.switch_to.window(driver.window_handles[-1])
You may find it helpful to keep track of the windows with vars:
main_window = driver.current_window_handle
page2_window = driver.window_handles[-1]
driver.switch_to.window(page2_window)
Note that when you want to close the new window/tab, you have to both close & switch back:
driver.close()
driver.switch_to.window(main_window)
I want to click on the Next-button at https://free-proxy-list.net/. The XPATH selector is //*[#id="proxylisttable_next"]/a
I do this with the following piece of code:
element = WebDriverWait(driver, 2, poll_frequency = 0.1).until
(EC.visibility_of_element_located((By.XPATH, '//*[#id="proxylisttable_next"]/a')))
if (element.is_enabled() == True) and (element.is_displayed() == True):
element.click()
print "next button located and clicked" # printed in case of success
Subsequently, I get all the IPs from the table like this:
IPs = WebDriverWait(driver, 2, poll_frequency = 0.1).until
(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ':nth-child(n) > td:nth-child(1)')))
Although the CSS_selector is the same for all tabs, and although I get a next button located and clicked, the IPs output is the same for both tabs (i.e. it seems like the Next-button never was clicked). Additionally, there is no Exception thrown.
Therefore, there must be something fundamentally wrong with my approach.
How to click on visible & enabled buttons correctly in phantomJS using python/selenium?
For your understanding, here is the html of the page section I am referring to:
As far as I see there could be two possible causes:
The click was not registered, though this is highly unlikely. You can look at other ways to click like JavascriptExecutor's click.
(Most likely) The find elements are queried right after the click is performed and before the Page 2 results are loaded. Since elements is visible from page 1, it exits immediately with the list of elements from page 1. An ideal way of doing this would be (using psuedocode as I am not familiar with python)
a. Get the current page number
b. Get all the IPs from the current page
c. Click Next
d. Check if (Current page + 1 ) page has become active (class 'active' is added to the Number 2)
e. Get all the elements from the current page
I am the OP and for anyone coming across a similar problem - The Next element was no longer attached to the DOM following its selection, which caused StaleElementReferenceException when printing element.is_enabled() or when clicking it - a detailed solution can be found here