Issue Selenium (python) unable to locate element - python

I am trying to click in a button of a website that is in a dropdown menu. I've tried different approach (xpath, element, link text.. ) but no one of these works and I am getting as error "unable to locate element"
The html code of the button is:
<a class="yuimenuitemlabel" href="javascript:exportToCSV()"> <span class="menu-text">Export All: CSV</span> </a> </li>
I tried the below approaches without luck:
browser.find_element_by_xpath("//*[#id='csv-export-item']/a/span").click()
browser.find_element_by_link_text('Export All: CSV')
browser.find_element_by_css_selector('span.content').click()
Any idea what could I do here? Thanks a lot.

Try this xpath once
//span[contains(text(),'Export All: CSV')]
Thank You,
Murali

Try with the following xpath method call once
browser.find_element_by_xpath("//*[text()='Export All: CSV']")
Hope it helps.

Related

Cannot open Instagram Followers page with Python

I need to open Instagram Followers Page with Python. I have already tried several ways to do it, but have no results... Can someone help me, please?
First I tried to do it this way:
# Go to the Group
driver.get('https://www.instagram.com/biblio_com/')
time.sleep(5)
driver.find_element_by_xpath('//a[#href="/biblio_com/followers/"]').click()
But it gives an error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <a class=" _81NM2" href="/biblio_com/followers/"> could not be scrolled into view
Then I tried the following:
# WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, '//a[#href="/biblio_com/followers/"]'))).click()
But it gives the same error ... (((
Another way I was trying to solve this problem was:
followers_link = driver.find_element_by_xpath('//a[#href="/biblio_com/followers/"]')
ActionChains(driver).move_to_element(followers_link).click(followers_link).perform()
It gives no error. But it also gives no result ...
Can someone help me with this?
driver.find_element_by_xpath('//a[#href="/biblio_com/followers/"]').click()
I tried with chromedriver and for me it is working. What browser are you using?

Printing Text from 2nd Div in Class in Python + Selenium

newbie here trying to learn Selenium. I have the following HTML Code:
<div class="lower-text">
<div data-text="winScreen.yourCodeIs">Your Code Is:</div>
<div>OUTPUTCODE</div>
</div>
I am trying to only print the text OUTPUTCODE, however the following code only prints "Your Code Is:".
text = browser.find_elements_by_class_name('lower-text')
for test in text:
print(test.text)
Any help would be appreciated. Thank you.
Try the below xpath.
//div[#class='lower-text']/div[last()]
You code should be
print(driver.find_element_by_xpath("//div[#class='lower-text']/div[last()]").text)
Try below Solutions:
1. Xpath :
//div[#class='gs_copied']
2. CSS selector
.lower-text > div:nth-child(2)
Your site is unstable and not always generating coupon code.Currently I am getting below error(check screenshot). So wont able to identify elements which i have mentioned above.
You need to amend your logic based on functionality and if person is Unlucky for getting coupon code then you have to write script to handle other functionality based on your site, (e.g: Check out our Hot Deals Page)
Try the following approach:
text = driver.find_element_by_xpath("//div[text()='Your Code Is:']//following-sibling::div[text()]").get_attribute('innerHTML')
print(text)
I have copy pasted your html part in a new text file and tried the following xpath which work perfectly:
//div[#class='lower-text']/div[text()='Your Code Is:']/following-sibling::div
Attaching screenshot link also. Please have a look and hopefully it will solve your problem.
https://imgur.com/EujgZrI

How to pull a changing class name with Python and Selenium?

I'm testing my web scraping skills with Python and Selenium and I found an button with a changing "id" "ember" and the numbers change everytime. Everything else is the same as all buttons. The only thing that's unqiue is
<button aria-label="View only People results" id="ember697" class="search-vertical-filter__filter-item-button artdeco-button artdeco-button--muted artdeco-button--2 artdeco-button--tertiary ember-view" type="button"><!---->
<span class="artdeco-button__text">
People
</span></button>
I've tried all the methods so far (i.e., id, CSS_selector, xpath, etc.).
[![Linked In button][1]][1]
Here's the error I keep getting no matter what I select.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
So, since you want to search through aria-label, you can use an XPATH:
driver.find_elements_by_xpath("//button[#aria-label='View only People results']")
EDIT: You could also use a css selector like this:
driver.find_elements_by_css_selector("button[aria-label='View only People results'']")
Try this:
option1 - i am assuming ember will be there and only numbers are changing
driver.find_elements_by_xpath("//*[starts-with(#id, 'ember')]")
option 2-
driver.find_elements_by_xpath("//*[contains(#id, 'ember')")
option 3-
driver.find_elements_by_xpath("//*[contains(#class, 'search-vertical-filter__filter-item-button')")
Hope it helps you.

Selenium locate submit button

I am working on a selenium script in Python, where I am on this stage trying to locate a submit button.
HTML
<div class="submit-buttons">
<button class="submit" type="submit">Filter</button>
</div>
I've tried and this has not worked.
So I am out of solutions:
browser.find_element_by_partial_link_text('Filter').click()
browser.find_element_by_link_text('Filter').click()
browser.find_element_by_class_name('submit').click()
Try xpath solution:
driver.find_element_by_xpath('//div[#class="submit-buttons"]/button[#class="submit"]')
If it is still not identifying, the element might be inside a frame, and you have to switch to that frame before finding the element.
By link text or by partial link text locators are going to work with links only - a elements. Here you have a button. If you want to use the button text, use the following "by xpath" locator:
//button[. = "Filter"]
This code should work :
driver.find_element_by_xpath('//button[text()='Filter']')
driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();

Using Python and Selenium why am I unable to find link by link text?

I have a list webelement that has a bunch of links within it. The html looks like:
<li>
<span class="ss-icon"></span> Remove
<a href="/sessions/new"><span class="ss-icon"></span> Sign in to save items</a
...
When I try to do something like:
link = element.find_element_by_link_text('Sign in to save items')
I get an error that says:
NoSuchElementException: Message: Unable to locate element:
{"method":"link text","selector":"Sign in to save items"}
I have been able to find this link by instead doing a find_elements_by_tag_name('a') and then just using the link with the correct HREF, but I would like to understand why the first method fails.
It happened to me before that the find_element_by_link_text method sometimes works and sometimes doesn't work; even in a single case. I think it's not a reliable way to access elements; the best way is to use find_element_by_id.
But in your case, as I visit the page, there is no id to help you. Still you can try find_elements_by_xpath in 2 ways:
1- Accessing title: find_element_by_xpath["//a[contains(#title = 'Sign in to save items')]"]
2- Accessing text: find_element_by_xpath["//a[contains(text(), 'Sign in to save items')]"]
Hope it helps.
The problem is, most likely, in the extra spaces before or/and after the link text. You can still approach it with a "partial link text' match:
element.find_element_by_partial_link_text('Sign in to save items')

Categories

Resources