Currently I am learning to use selenium to automate testing. One of my task is to click to the next page on a website. The xpath I copied from the specific button is the following:
xpath = '//*[#id="pagination"]/div/div[1]/a[4]'
So when I use driver.find_element_by_xpath(xpath).click() it will bring me to the next page.
To click through multiple pages I would like to find the specified element based on the condition that the text is equal to the correct page.
For this I tried the following conditional xpath:
xpath = //*[#id="pagination"]/div/div[1]/a[text()='page_num']
where page_num is the specific page I want to click on.
Example:
for the follwing element I would use the xpath:
element = 2
xpath = //*[#id="pagination"]/div/div[1]/a[text()='2']
I would expect that selenium would click to the specified page but instead I get an Error message that the xpath doesn't exist.
What should be the correct conditional xpath name?
Related
Im using Python Selenium to scrape a website. At some point during the scrape i want to take a screenshot. I only 'roughly' want to take a screenshot covering specific WebElements. How do I take a screenshot of section containing multiple WebElements?
To avoid an eventual XY Problem, here is how you can screenshot any particular element you want, with Selenium (Python) - that element can be a div encompassing other elements:
[...]
url = 'https://www.startech.com.bd/benq-gw2480-fhd-monitor'
browser.get(url)
browser.execute_script('window.scrollBy(0, 100);')
elem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[#id='specification']")))
elem.screenshot('fullspec.png')
print('screenshotted specs')
Se Selenium documentation here.
so I wanted to click a checkbox on website using selenium (python).That's the button I want to click
So I thought that it would work with that code:
driver.find_element_by_xpath("//input[#name='termsCheck']").click()
But that gives me an errorThat's the error I get
Additional info: there are 2 more checkboxes on the same page which have also <span class="custom-checkbox"> ::before ::after </span>
Has anyone an idea how to get selenium to click the checkbox?
I have seen some scenarios were the element must be clicked with javascript because it is covered by other elements. Alternatively you could click the <span> element that is covering it.
Here is how to click the element with javascript using python and selenium. Since you have not provided the HTML I am assuming that the xpath you provided uniquely identifies the element you want to click.
element_to_click = driver.find_element_by_xpath("//input[#name='termsCheck']")
driver.execute_script("arguments[0].click();", element_to_click )
On most browsers you should be able to copy the XPath or CSS selector by right clicking the specific element on the developer tools console. The click() method should work.
The code is attempting to click the checkbox and Selenium API doesn't like that. The error informs about that, but is not specific enough. Try using auxiliary class Select instead:
from selenium.webdriver.support.ui import Select
element = driver.find_element_by_xpath("//input[#name='termsCheck']")
select = Select(element)
select.select_by_index(index)
Additionally, make sure that XPath //input[#name='termsCheck'] is only matching single element.
Refer to Selenium Python documentation for more details.
I am new to selenium coding, and I am seeing several xpaths that contain (.,'followed by something') what does the ., refer to?
The . character within the xpath is the short form of text()
As an example if an WebElement is represented within the DOM Tree as:
<span>Use this payment method</span>
Effective Locator Strategies will be:
xpath 1:
//span[text()='Use this payment method']
xpath 2:
//span[contains(., 'Use this payment method')]
Reference
You can find a couple of relevant discussions in:
How to locate the button element using Selenium through Python
While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
How does dot(.) in xpath to take multiple form in identifying an element and matching a text
Hello I am trying to click on a button within python selenium that has no ID. The button has the text Submit an application within it. However there are multiple phrases of "Submit an application" on the website. The attributes of the website are:
<a class= "button" href = "(a link)" analytics-on="click" analytics-category="Body" analytics-event="(a link)" analytics-label="Submit an application||(a link)">
According to the documentation, there are multiple methods you can use to locate an element.
In this situation, I would personally use Xpath if available. Open the chrome developer tool on the HTML tab and find the element you want to locate (or right click on the element and go to inspect element). Right click on the HTML tag (in the dev tool) and select copy > xpath.
Now in your python code you can locate your element like so:
button = driver.find_element_by_xpath("past_the_xpath_here")
Alternatively because you are trying to locate a link, you could also locate the element using the link text:
button = driver.find_element_by_link_text("Submit an application")
you can try with css_selector.
driver.find_element_by_css_selector('[analytics-on="click"]').click()
driver.find_element_by_css_selector('[analytics-category="Body"]').click()
or link_text method is also a good choice in locating an a tag,
driver.find_element_by_link_text("Submit an application").click()
I am trying to create an add to car bot which finds the item and selects the size and fills out the user billing and card information. I am currently stuck on the checkboxes of the site. I've tried to use the XPath of the checkbox and it gives me an error or it won't execute.
The website I am using is as below:
https://www.supremenewyork.com/checkout
Below is a picture of the checkout page with the checkbox
Here is the html elements used for the code
Below is my code that I used to get the program to find the checkbox element and use a .click() to select the box.
Checkboxes = browser.find_element_by_xpath('//*[#id="cart-cc"]/fieldset/p[2]/label/')
Terms = ActionChains(browser).move_to_element(Checkboxes).click()
Terms.perform()
use below code:
Webelement element = browser.find_element_by_xpath('//label[./div[#class="icheckbox_minimal"]/input[type="checkbox"]]/div/input');
element.click();