HTML:
<div class="__section-image" style="background-image: url("/img/all/05_element/continentImg/1.png");"></div>
at this html part how can I locate the background-image?
To get the background image you need to use value_of_css_property(property_name) method and inducing WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
import re
my_property = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class='__section-image']"))).value_of_css_property("background-image")
print(re.split('[()]',my_property)[1])
Using XPATH:
import re
my_property = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='__section-image']"))).value_of_css_property("background-image")
print(re.split('[()]',my_property)[1])
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related
When I call the <div> tag and his class value then it does not response.
HTML:
<div id="ccmgt_explicit_accept" class="privacy-prompt-button primary-button ccmgt_accept_button ">
<span>Accept All</span>
</div>
Snapshot of the element:
To click on Accept All you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#ccmgt_explicit_accept > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Accept All']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I want to go to the jobs section on LinkedIn with selenium, but it leads to the connections(networks) section with the same class name. how do i solve this problem?
My code;
jobs_sec = driver.find_element("xpath", "//a[#class='app-aware-link global-nav__primary-link']")
jobs_sec.click()
The tag reached when run;
The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.app-aware-link.global-nav__primary-link[href^='https://www.linkedin.com/jobs']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='app-aware-link global-nav__primary-link' and starts-with(#href, 'https://www.linkedin.com/jobs')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".app-aware-link.global-nav__primary-link")))
jobs_sec.click()
If that doesn't work, try as an alternative:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#data-nnt-old-href='https://www.linkedin.com/jobs/?']")))
jobs_sec.click()
You will also need to import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
How to click this button:
I have tried the following:
sumbitbutton = driver.find_element(By.XPATH, "//div[text() = 'mt8 mb8']")
I decided to insted abandon this method and go for a nuther as it lead to a lot of other problums latter down the line
You will want something like:
button = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "inputButton.main_submit")))
button.click()
As you did not confirm the URL, I cannot test it, but it should work.
Also, do not forget to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Generally <div> tags aren't clickable. Additionally mt8 mb8 are the classanmes, not the text.
Solution
As per the HTML:
to click on the <input> element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.inputButton.main_submit[value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='inputButton.main_submit' and #value='Submit']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I'm trying to get the text inside of <pre> tag and I have tried with get_attribute('text'), get_attribute('value'), .text(), .value(), get_attribute("innerHTML") but I keep failing:
Snapshot:
This is the code that i'm using:
import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("chromedriver.exe")
driver.get("###")
elem=driver.find_element(By.ID, "login_admin").click()
elem=driver.find_element(By.XPATH, "/html/body/div[15]/div[2]/form/div[1]/input").send_keys("###")
elem=driver.find_element(By.XPATH, "/html/body/div[15]/div[2]/form/div[2]/input").send_keys("###")
elem=driver.find_element(By.XPATH, "/html/body/div[15]/div[3]/div/button[1]").click()
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/nav/div/div[1]/button/span[3]').click()
time.sleep(2)
elem = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/nav/div/div[2]/ul/li/a')))
time.sleep(2)
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/nav/div/div[2]/ul/li/a').click()
time.sleep(2)
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/div/div/div/div/div/div/div[2]/div/div/div/div/div/div/div[2]/div/span[1]/input[2]').send_keys('###')
time.sleep(1)
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/div/div/div/div/div/div/div[2]/div/div/div/div/div/div/div[2]/div/span[2]/button').click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='output']/pre[text()]"))).get_attribute("innerHTML"))
And this is what it says when print:
To print the text within the <pre> tag you can use either of the following locator strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "div.output > pre").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//div[#class='output']/pre[contains(., 'ContactUri')]").text)
To extract the text ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.output > pre"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='output']/pre[contains(., 'ContactUri')]"))).get_attribute("innerHTML"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
I have this line of code which I am trying to use to obtain the status of an item. Here is the line of code:
item_status = driver.findElement(By.className("status-info")).getText();
I'm not sure how I can adjust this to retrieve the text seen here:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options=Options()
driver=webdriver.Chrome(options=options)
#Directing to site
driver.get("https://www.amazon.co.uk/Nintendo-Switch-OLED-Model-Neon/dp/B098TNW7NM/ref=sr_1_3?keywords=Nintendo+Switch&qid=1651147043&sr=8-3");
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/span/form/div[3]/span[1]/span/input"))).click()
When you are doing this
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/header/div/div[1]/div[2]/div/form/div[3]/div/span/input"))).click()
it will click on search icon, now on the result page, this xpath //span[#class='a-size-base a-color-success a-text-bold'] is not present hence nothing is getting printed on the console you are likely to face TimedoutException.
However looking at the screenshot that you've shared, I would say to use this xpath
//div[#id='availability']//span[contains(text(),'In stock.')]
If you want to print the text and tag
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerHTML"))
If only text you want:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerText"))
driver.findElement(By.className("status-info")) is the Java syntax and getText() is a Java method. Possibly you need Python syntax and method.
Solution
To print the text In stock. you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.a-size-base.a-color-success.a-text-bold"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='a-size-base a-color-success a-text-bold']"))).get_attribute("innerHTML"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC