I am struggling with a problem in Selenium using Python.
This is a dummy draft of what I have.
<body>
<button info="content1" aria-label="1">"Click 1"</button>
<button info="content1" aria-label="2">"Click 2"</button>
<button info="content2" aria-label="2">"Click 2"</button>
<button info="content2" aria-label="4">"Click 4"</button>
<body>
My target is to select the button that has info="content1" and aria-label="2"
I have already tried
element=driver.find_element_by_css_selector('button[info="content1"] and button[aria-label="2"]')
But doesn't work and returns NoSuchElementException
Would you please help me?
Simply put the two bracketed attribute selectors next to each other with no and:
element = driver.find_element_by_css_selector('button[info="content1"][aria-label="2"]')
Related
I am including portions of the HTML below. I believe I have found the larger element using the command:
driver.find_element_by_xpath('//div[#id="day-number-4"]')
That div ID is unique on the web page, and the command above did not create an exception nor error.
The HTML code that defines that element is:
<div id="day-number-4" class="c-schedule-calendar__class-schedule-content tabcontent js-class-schedule-content u-is-none u-is-block" data-index="3">
Now, the hard part. Inside that div element are a number of "<li"'s that take the form of:
<li tabindex="0" class="row c-schedule-calendar__class-schedule-listitem-wrapper c-schedule-calendar__workout-schedule-list-item" data-index="0" data-workout-id="205687" data-club-id="229">
and then followed a clickable button in this format:
<button class="c-btn-outlined class-action" data-class-action="book-class" data-class-action-step="class-action-confirmation" data-workout-id="205687" data-club-id="229" data-waitlistable="true"><span class="c-btn__label">Join Waitlist</span></button>
I always want to click on the 3rd button inside that <div element. The only thing unique would be the data-index, which starts at 0 for the 1st <li", and 2 for the 3rd So I want to find the clickable button that will follow this HTML code:
<li tabindex="0" class="row c-schedule-calendar__class-schedule-listitem-wrapper c-schedule-calendar__workout-schedule-list-item" data-index="2" data-workout-id="206706" data-club-id="229">
I cannot search on data-index as "data-index="2"" appears many times on the web page.
How do I do this?
I answered my own question. I used multiple searches within the specific element using this code:
Day_of_Timeslot = driver.find_element_by_xpath('//div[#id="day-number-4"]')
Precise_Timeslot = Day_of_Timeslot.find_element_by_xpath(".//li[#data-index='1']")
Actual_Button = Precise_Timeslot.find_element_by_xpath(".//button[#data-class-action='book-class']").click()
I have a problem with this checkbox. I tried to click searching element with id, name, XPath, CSS Selector and contains text and still I could not click on this checkbox. Additionally, I've tried with another site with similar HTML code and on this site, it was enough to look for id and click. Any ideas?
<div class="agree-box-term">
<input tabindex="75" id="agree" name="agree" type="checkbox" value="1">
<label for="agree" class="checkbox-special">* Zapoznałam/em się z Regulaminem sklepu internetowego i akceptuję jego postanowienia.<br></label>
</div>
Here is my Python code https://codeshare.io/5zo0Jj
I have used javaScript Executor and it clicks on the element.However I have also checked webdriver click is not working.
driver.execute_script("arguments[0].click();", driver.find_element_by_id("agree"))
I don't know why this is, but in my experience some boxes don't accept click but do accept a 'mousedown' trigger.
try:
driver.execute_script('$("div.agree-box-term input#agree").trigger("mousedown")')
This solution does rely on jquery being on the page, if it's not we can write it in javascript
r = driver.find_element_by_xpath("//*[#id="form-order"]/div[2]/div[4]/label")
r.click()
Does this work for you? Sometimes it's just a question of selecting the right xpath, or adding the brackets after click.
Does your code contain nested html tags? For example:
<html>
<div>
<p> Some text </p>
<html>
That block can't be traversed!
</html>
</div>
</html>
Anything inside the second HTML tags can't be traversed/accessed. Try to see if that's the case.
In any other case the following code ran perfectly fine for your snippet:
driver.find_element_by_css_selector('#agree').click()
I have a HTML page containing a form with an tag. I want to set the value of the drop down in this tag using Selenium.
This is how I retrieve the input element:
driver.find_element_by_xpath("/html/body/div[2]/div/div/form/div/div[1]/div[3]/div[1]/div/div[1]/input")
I tried to set the value using select_month.send_keys("09") but this is not accepted by the web page when I try to submit the form so I need to find another method.
EDIT: Here is the HTML of the form, I have ensured that it is the right element in my x-path:
<input autocomplete="off" tabindex="-1" class="ui-select-search ui-select-toggle ng-pristine ng-valid ng-touched" ng-click="$select.toggle($event)" placeholder="Select month" ng-model="$select.search" ng-hide="!$select.searchEnabled || ($select.selected && !$select.open)" ng-disabled="$select.disabled" type="text">
After messing around a bit and incorporating the better practice presented by alecxe, this solution worked...
driver.find_element_by_xpath("//input[#placeholder='Select month']").click()
driver.find_element_by_xpath("//*[contains(text(), '09')]").click()
I'm migrating some Selenium WebDriver test cases from unittest to py.test and seeing an issue when I attempt to use the *:contains() command on a CSS Selector. Here's an example of a line that fails:
WebDriverWait(driver, 60).until(
expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "body.yui-skin-sam div[id=Navigation] ul[id=mainNav] li span:contains(Home)"))
)
Any time I've attempt this command with a CSS Selector that does not contain *:contains(), it works fine. Otherwise, I get either a TimeoutException or a InvalidElementStateException. I've also tried using the following instead:
expected_conditions.element_located_to_be_selected
" .element_to_be_clickable
" .element_to_be_selected
" .presence_of_element_located
" .text_to_be_present_in_element
" .visibility_of_element_located
I'm fairly sure my issue has more to do with attempting to use *:contains(). Attempting to use By.NAME, By.XPATH, or any other object name, it works fine. Here's what I'm looking at with respect to the HTML Source on the page:
</div>
<div id="HeaderBox">
<div id="Content">
<div id="Header">
<div id="Left">
<img src="/images/header_logo.png" />
</div>
<div id="Right">
<div id="Text">
<div id='active_conf'> running... </div><br>
Profile is unlocked. ( Lock
| Logout )<br>
</div>
</div>
</div>
<div id="Navigation">
<ul id="mainNav">
<li><span>Home</span></li>
<li><span>DataValues</span></li>
<li><span>Devices</span></li>
</ul>
Does anyone know of a way I can get this to work? Thanks ahead of time!
I'll preface this by saying I'm not very familiar with the Python bindings for Selenium (I use Java)... However, I had this same problem a while back. I was able to solve this by injecting Sizzle.js onto the DOM to aid element location. Sizzle brings with it the :contains() psuedo-class and support for other JQuery selectors as well.
I was able to find a small bit of info for Python HERE, but I'm sure there's more thorough information out there.
I was able to find a solution to this issue. For starters, as #Mark Rowlands mentioned above, contains() can't be used. I needed to make some changes to my CSS syntax as well so it would do a better job of pointing at the object I wanted to verify. Finally, since I have an attribute, a located, and a value [where the value is the text located in contains()], I needed a method that allowed for all 3, which in this case was the text_to_be_present_in_element method. So, to locate Home, I needed to do the following:
WebDriverWait(driver, 60).until(
expected_conditions.text_to_be_present_in_element((By.CSS_SELECTOR, "css=ul[id='mainNav'] > li:nth-of-type(1) > a > span"), "Home")
)
I also could have used a > span instead of the long form listed and probably could substitute nth-of-type for first-child. Now, if I want to locate one of the other values, devices for example, I needed to do the following:
WebDriverWait(driver, 60).until(
expected_conditions.text_to_be_present_in_element((By.CSS_SELECTOR, "ul[id='mainNav'] > li:nth-of-type(2) > a > span"), "DataValues")
)
While I'm sure there are other solutions, these are the ones that worked for me. Hope this helps someone out in the future.
I am trying to search for an element in a sub-element with Selenium (Version 2.28.0), but selenium des not seem to limit its search to the sub-element. Am I doing this wrong or is there a way to use element.find to search a sub-element?
For an example I created a simple test webpage with this code:
<!DOCTYPE html>
<html>
<body>
<div class=div title=div1>
<h1>My First Heading</h1>
<p class='test'>My first paragraph.</p>
</div>
<div class=div title=div2>
<h1>My Second Heading</h1>
<p class='test'>My second paragraph.</p>
</div>
<div class=div title=div3>
<h1>My Third Heading</h1>
<p class='test'>My third paragraph.</p>
</div>
</body>
</html>
My python (Version 2.6) code looks like this:
from selenium import webdriver
driver = webdriver.Firefox()
# Open the test page with this instance of Firefox
# element2 gets the second division as a web element
element2 = driver.find_element_by_xpath("//div[#title='div2']")
# Search second division for a paragraph with a class of 'test' and print the content
print element2.find_element_by_xpath("//p[#class='test']").text
# expected output: "My second paragraph."
# actual output: "My first paragraph."
If I run:
print element2.get_attribute('innerHTML')
It returns the html from the second division. So selenium is not limiting its search to element2.
I would like to be able to find a sub-element of element2. This post suggests my code should work Selenium WebDriver access a sub element but his problem was caused by a time-out issue.
Can anyone help me understand what is happening here?
If you start an XPath expression with //, it begins searching from the root of document. To search relative to a particular element, you should prepend the expression with . instead:
element2 = driver.find_element_by_xpath("//div[#title='div2']")
element2.find_element_by_xpath(".//p[#class='test']").text
Use the following:
element2 = driver.find_element_by_cssselector("css=div[title='div2']")
element2.find_element_by_cssselector("p[#class='test']").text
Please let me know if you have any problems.
I guess,we need use method "By" from webdriver.common.by when use "driver.find_element".
So...the code must be:
from selenium import webdriver
driver = webdriver.Firefox()
from selenium.webdriver.common.by import By
element2 = driver.find_element(By.XPATH, "//div[#title='div2']")
element2.find_element(By.XPATH, ".//p[#class='test']").text
This is how you search for element or tag in CSS subclass and I believe that it works for multilevel situation as well:
Sample HTML:
<li class="meta-item">
<span class="label">Posted:</span>
<time class="value" datetime="2019-03-22T09:46:24+01:00" pubdate="pubdate">22.03.2019 u 09:46</time>
</li>
This is how you would get pubdate tag value for example.
published = driver.find_element_by_css_selector('li>time').get_attribute('datetime')
Chrome Webdriver :
element = driver.find_element_by_id("ParentElement")
localElement = element.find_element_by_id("ChildElement")
print(localElement.text)
Find The Child of any Elements
parent = browser.find_element(by=By.XPATH,value='value of XPATH of Parents')
child=parent.find_elements(by=By.TAG_NAME,value='value of child path')