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
Related
New coder here. I've been trying to scrape just one piece of text on a very java based website for a while now using Selenium. Not sure what I am doing wrong that this point.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://explorer.helium.com/accounts/13pm9juR7WPjAf7EVWgq5EQAaRTppu2EE7ReuEL9jpkHQMJCjn9")
earnings = driver.find_elements_by_class_name('text-base text-gray-600 mb-1 tracking-tight w-full break-all')
print(earnings)
driver.quit()
Image of attempted element to scrape :
I am trying to scrape that dollar amount in this container so I can eventually use it in a daily report that I am building.
Everything I have tried has resulted in it returning none. Even when I try to grab the text from that element.
Here is website link: https://explorer.helium.com/accounts/13pm9juR7WPjAf7EVWgq5EQAaRTppu2EE7ReuEL9jpkHQMJCjn9
You should wait until javascript loads, page loads, elements loads.
_ = driver.Manage().Timeouts().ImplicitWait;
You can create condition until element appers.
ExpectedConditions ...... define selenium conditions
//This is how we specify the condition to wait on.
wait.until(ExpectedConditions.alertIsPresent());
You can use XPATH ! The DOLLAR XPATH IS
/html/body/div[1]/div/article/div[2]/div/div[2]/div/div[2]/div[1]/div[2]/div[2]
FIREFOX XPATH FINDER
https://addons.mozilla.org/en-US/firefox/addon/xpath_finder/
You can use this xpath
//*[#id="app"]/article/div[2]/div/div[2]/div/div[2]/div[3]/div[1]/div[1]/div[3]
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 need to find the webelements like id="rcmrowgeneral".
A standard driver.find_element_by_id() is not working.
It's like they are inside another HTML page.
How can I find them with selenium to interact with them?
You can do this by following the selenium syntax which states if you want to find a specific tag inside an iframe you have to switch to that iframe first and then you can use selenium query to find it. Since you've not posted any code over here i assume you've background knowledge of handling automation processes. You can do this task by following this naming convention:
driver.switchTo().frame("id or name of the element")
driver.find_element_by_id("your id here")
Your element is inside of an iframe. First you need to switch to the iframe and then it is a good practice to wait for the element to be visible/clickable before interacting with it.
driver.switch_to().frame(driver.find_element_by_id("TiscaliWebmailFrame"))
WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.ID, "rcmrowgeneral"))).click()
I am trying to click the search button in this link here
I would like to click the search button and then download all URL's on the next page but currently it is finding monthly list
The link below takes you to a screenshot of my code where it outputs the monthly list button instead of searcj/
Python code selenium
Welcome to Stack Overflow!
You can use the .click() method to click an element object, and the .find_element_by_xpath() method to find the element. I've located that the element's full XPATH is /html/body/div/div/div[3]/div[3]/div/form/fieldset/div[5]/input[2].
You can implement all the pieces together like so:
driver.find_element_by_xpath("/html/body/div/div/div[3]/div[3]/div/form/fieldset/div[5]/input[2]").click()
if you are starting I recommend you using some extension to help you find the xPath of the elements.
I used Xpath Helper from Chrome, but you can use any other.
The Xpath of the Search button is:
/html[#class='js']/body/div[#id='idox']/div[#id='pa']/div[#class='container'][2]/div[#class='content']/div[#class='tabcontainer']/form[#id='weeklyListForm']/fieldset/div[#class='buttons']/input[#class='button primary']
After you have the XPath you can use it in the selenium driver to find the elements.
This can be enough, but I recommend you to learn in depth how this works to know exactly what it is doing.
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?