How can I click on this element using selenium? I was tried using:(but was not successful)
divselected = driver.find_element_by_css_selector(div[aria-label="Add a public comment..."])
divselected.click()
I also tried
divselected = driver.find_element_by_id("contenteditable-root")
divselected.click()
The div I am trying to click:
<div id="contenteditable-root" contenteditable="true" dir="auto" class="style-scope yt-formatted-string" aria-label="Add a public comment..."></div>'
The following code works:
divselected = driver.find_elements_by_xpath("//*[contains(text(), 'Add a public comment...')]")
divselected[0].click()
Hope this help:
divselected=driver.find_element_by_xpath("//*[contains(text(),'Add a public comment')]")
incorrect value for the id. The corrected code uses the correct id value, contenteditable-root to #contenteditable-root
try this code :
divselected = driver.find_element_by_css_selector("#contenteditable-root")
divselected.click()
This code finds the element with the id attribute contenteditable-root using a CSS selector and then performs a click action on it using the click() method.
try using
driver.find_element(By.ID, 'contenteditable-root').click()
or instead of ID use Class Name.
Related
Trying to scrape a website, I created a loop and was able to locate all the elements. My problem is, that the next button id changes on every page. So I can not use the id as a locator.
This is the next button on page 1:
<a rel="nofollow" id="f_c7" href="#" class="nextLink jasty-link"></a>
And this is the next button on page 2:
<a rel="nofollow" id="f_c9" href="#" class="nextLink jasty-link"></a>
Idea:
next_button = browser.find_elements_by_class_name("nextLink jasty-link")
next_button.click
I get this error message:
Message: no such element: Unable to locate element
The problem here might be that there are two next buttons on the page.
So I tried to create a list but the list is empty.
next_buttons = browser.find_elements_by_class_name("nextLink jasty-link")
print(next_buttons)
Any idea on how to solve my problem? Would really appreciate it.
This is the website:
https://fazarchiv.faz.net/faz-portal/faz-archiv?q=Kryptow%C3%A4hrungen&source=&max=10&sort=&offset=0&_ts=1657629187558#hitlist
There are two issues in my opinion:
Depending from where you try to access the site there is a cookie banner that will get the click, so you may have to accept it first:
browser.find_element_by_class_name('cb-enable').click()
To locate a single element, one of the both next buttons, it doeas not matter, use browser.find_element() instead of browser.find_elements().
Selecting your element by multiple class names use xpath:
next_button = browser.find_element(By.XPATH, '//a[contains(#class, "nextLink jasty-link")]')
or css selectors:
next_button = browser.find_element(By.CSS_SELECTOR, '.nextLink.jasty-link')
Note: To avoid DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() import in addition from selenium.webdriver.common.by import By
You can't get elements by multiple class names. So, you can use find_elements_by_css_selector instead.
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
print(next_buttons)
You can then loop through the list and click the buttons:
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
for button in next_buttons:
button.click()
Try below xPath
//a[contains(#class, 'step jasty-link')]/following-sibling::a
I am trying to retrieve text from a button type webelement using Python scripting in Selenium
The HTML code of the button looks like:
<button class="list-item ng-binding" ng-click="selectLineFilter(line)" type="button">
<i class="mdi mdi-domain blue" aria-hidden="true"></i>
12063497545
</button>
How can I retrieve the text. I used .text, it returns an error as " 'list' object has no attribute 'text' ". Please Help.
I think you are using find_elements which returns list of WebElement while .text works on single WebElement, So you should try using find_element as below :-
driver.find_element_by_css_selector("button.list-item.ng-binding[type='button']").text
Or if you want to find all elements text with the same locator, you should try using find_elements and iterate through in loop as below :-
buttons= driver.find_elements_by_css_selector("button.list-item.ng-binding[type='button']")
for button in buttons:
button.text
use .GetAttribute("innerHTML")
you can use the concept of this Java code :
WebDriver driver;
String a = driver.findelement(by.xpath("")).getText(); //use css or id attribute
System.out.println(a); //this will print the text present on the button.
I am trying to use the Python Selenium API in order to click on a button. The HTML code is as follows:
<button class="btn wizard-next btn-primary" type="button">Weiter</button>
How to best identify this element? I was trying the following code
driver.find_element_by_class_name("btn wizard-next btn-primary").click()
but got an error
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
What else can I do to select this element?
You cannot use find_element_by_class_name() if class name value contains spaces.
Try:
driver.find_element_by_xpath("//button[#class='btn wizard-next btn-primary']").click()
You can use css selector also
driver.find_element_by_css_selector("btn").click()
I'm trying to get Selenium to click on View All Companies button, but i'm not sure what am I doing wrong. It returns no element found
html code
<div class="screener-toggles">
<div class="buttons">
<span class="button selected" data-name="advanced-screener">Search by Screener<span data-name="advanced-screener" class="arrow selected"></span></span>
<span class="button" data-name="alpha-factors">Search by Alpha Factors<span data-name="alpha-factors" class="arrow"></span></span>
<span class="button" data-name="all-companies">View All Companies<span data-name="all-companies" class="arrow"></span></span>
</div>
</div>
python code I wrote
element1 = driver.find_elements_by_class_name('View All Companies')
element1.click()
# I have tried all-companies instead of View All Companies as well. But still doesn't work
Should I not be using find_elements_by_class_name?
Any advice on what I am doing wrong is greatly appreciated!
try xpath: "//span[contains(text(),'View All Companies')]"
View All Companies is text, not the class. Try looking by text with css_selector or xpath
element1 = find_element_by_css_selector('span:contains("View All Companies")')
element1 = find_element_by_xpath('//span[contains(text(), "View All Companies")]')
Or by the data-name attribute which contains all-companies
element1 = find_element_by_css_selector('span[data-name*="all-companies"]')
Yes, you should not use the find_elements_by_class_name instead of use find_element_by_class_name.
find_elements_by_class_name is used when your expecting your locator to return more than 1 element. for a specific element use only find_element_by_class_name.
Another thing is I am not able to see any class name as View All Companies in your HTML code. Please look into your HTML and select classname or other locator carefully
Hope it will help you
I am testing a website that has buttons that are not discernible by element or id. However, I am able to identify it by xpath or css class. How do you click on a button using one of those two attributes. I'm able to assert the button is there using get_element_by_xpath; however, I seem unable to use the click_element, click_link, or click_button using id_or_elem. What statement would you use to click on a button like this?
Edit:
The test code looks like this: (webpage name removed on purpose)
class TestMyTest(cases.SSTTestCase):
def test_mytestcase_home_page(self):
go_to('http://www.mywebpage.com')
assert_title_contains('Page Name')
assert_element(tag='a', text='Log in')
click_element(id_or_elem='page_nav')
assert_title_contains('Login')
assert_element(id='tbLUser')
write_textfield('tbLUser','username')
assert_element(id='tbLPass')
write_textfield('tbLPass','badpassword')
assert_element(css_class='btn-login1')
get_element_by_xpath('//*[#id="login_box"]/div/a/img')
The code I'm trying to test from:
<div class="btn-login1">
<a href="javascript:void(0)" onclick=javascript:return Validate(document.theLoginForm,'/mypage/mywebpage.asp');" onmouseout="MM_swapImgRestore();" onmouseover="MM_swapImage('lg5_login','','/assets/page4/lg5_login_f2.png',1);">
<img name="lg5_login" src="http://www.mywebpage.com/login/lg5_login.png" border="0" alt="Log in Now">
</a>
</div>
If this answer is helpful to someone else. We we able to solve the issue with the following solutions.
click_element(get_element_by_xpath('your xpath'), wait=True)
click_link(get_element_by_xpath('your xpath'), wait=True)
click_button(get_element_by_xpath('your xpath'), wait=True)
Parameters: id_or_elem The identifier of the element, or its element object.
All of the click actions should support id or element