<div class="clearfix entry_but_bar_out clearfix" style="display: none;">
<div class="entry_but_bar_in clearfix">‹ yanıtlaeditsilmesajgammazla#208281235<i class="ssicon-facebook"></i>
<i class="ssicon-twitter"></i>
</div><!-- entry_but_bar_in clearfix -->
</div>
i'm trying to make elements visible and click on it . But it doesn't work ,
clicking = browser.find_element_by_xpath('//*[#id="middle-block"]/ol/li/div/div[3]/div[2]/span')
browser.execute_script("removeAttribute('display:none');",clicking)
clicking.click()
Please try the below code and let me know if it works for you.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
# Put your URL
driver.get('Your URL')
time.sleep(5)
Target_Div = driver.find_element_by_xpath('//div[contains(#class,"clearfix entry_but_bar_out clearfix")]')
driver.execute_script("arguments[0].setAttribute('style','display:\"true\"')", Target_Div)
Use the above code and make correction in your code.
Related
I have a piece of HTML Code, which offers you to put your email in an input field after hitting a button (after clicking the button the textfield appears):
<div class="column-left w-col w-col-5">
<label class="radio-button-field-2 left w-radio">
<input type="radio" id="paypal" name="payout" value="paypal" data-name="payout" required="" class="w-form-formradioinput radio-button w-radio-input">
<span for="paypal" class="radio-button-label-2 w-form-label">PayPal-Konto</span>
</label>
</div>
<div class="block-payout-paypal" style="">
<input type="email" class="text-field-2 w-input" maxlength="256" name="paypal-email-2" data-name="Paypal Email 2" placeholder="max.moritz#beispiel.com" id="paypal-email" required="">
</div>
My Goal now is to write a script which allows me to click on the button and then file out the input field. The website is: https://gruenkraft-zufriedenheit.de/ And the button I'm looking for is called: PayPal-Konto
When (PayPal-Konto) button is clicked, you would see a input, you can locate that with the help of below code.
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://gruenkraft-zufriedenheit.de/")
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='PayPal-Konto']"))).click()#ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='PayPal-Konto']")))).click().perform()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='paypal-email-2']"))).send_keys('some-email#gmail.com')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
This worked for me:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://gruenkraft-zufriedenheit.de/ ')
element = driver.find_element_by_tag_name('button_tag_name_youre_looking_for')
element.click()
from then I use beautifulsoup to go on with what I needed to do
Hope this helps : )
You can try with below code too.
driver.find_element_by_xpath("//span[text()='PayPal-Konto']").click()
driver.find_element_by_id("paypal-email").send_keys("text")
Updated as per comments.
I can't seem to target this link with find_element_by_class_name()
<div class="searchResult-gridItem">
<div class="searchResult" data-test="searchResult" role="none" tabindex="-1">
<a data-testid="vehicle-card-link-box" href="/us/en/car-rental/united-states/hillside-
nj/tesla/model-3/802284?searchId=g0WoIMHS" target="_blank" rel="nofollow" class="css-
8ydh9n-VehicleCardLinkBox/div>
</div>
the link I'm trying to target is /us/en/car-rental/united-states/hillside-nj/tesla/model-3/802284?searchId=g0WoIMHS in the above.
I have tried:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
browser = webdriver.Chrome(r'PATH\chromedriver.exe')
time.sleep(5)
browser.get('https://turo.com/us/en/search?country=US&defaultZoomLevel=11&delivery=false&endDate=09%2F25%2F2021&endTime=10%3A00&international=true&isMapSearch=false&itemsPerPage=200&latitude=40.735657&location=newark%2C%20nj&locationType=City&longitude=-74.1723667&maximumDistanceInMiles=50®ion=NJ&sortType=RELEVANCE&startDate=09%2F22%2F2021&startTime=10%3A00')
time.sleep(10)
button = browser.find_element_by_partial_link_text('/us/en/car-rental/')
button.click()
but the code throws an error that the element can't be found.
i was able to successfully identify the element by xpath:
button = browser.find_element_by_xpath("//*[#data-testid='vehicle-card-link-box']")
I'm trying to find an element by xpath, but it does not return anything.
It seems all find_elements methods work except xpath.
I'm trying to find the text Ponderosa Campground which is clickable.
def searchCondition():
elem = driver.find_elements_by_xpath("//div[#aria-label='PONDEROSA CAMPGROUND']")
elem.click()
here is the URL where I'm trying to find the element.
https://www.recreation.gov/search?q=Ponderosa%20Campground
<div data-component="FocusManager" tabindex="-1" style="outline: none;">
<div class="flex-grid search-outer-wrap" data-component="FlexRow">
<div class="flex-col-12" data-component="FlexCol">
<div class="rec-flex-card-wrap " id="rec-card-233118_campground">
<a data-card="true" class="rec-flex-card-image-wrap" href="/camping/campgrounds/233118" target="_blank" rel="noopener noreferrer" alt="PONDEROSA CAMPGROUND">
<div data-component="FauxImage" class="sarsa-faux-image rec-flex-card-image-wrap-faux-image" role="img" aria-label="PONDEROSA CAMPGROUND" style="min-height: 155px; background-image: url("https://cdn.recreation.gov/public/images/76799.jpg");"></div></a>
<div class="rec-flex-card-content-wrap"><a href="/camping/campgrounds/233118" target="_blank" rel="noopener noreferrer" aria-label="PONDEROSA CAMPGROUND - 2.6 stars / $25 / night">
Here is the full script.
import time
from time import sleep
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import pause
import pyperclip
# Definitions
ID = ""
PW = ""
SRCH = "Ponderosa Campground"
URL = "https://www.recreation.gov/search?q=Ponderosa%20Campground"
now = datetime.now()
options = Options()
options.headless = False
# executable_path for webdriver
driver = webdriver.Chrome(
executable_path='C:/chromedriver.exe',
options=options)
wait = WebDriverWait(driver, 50)
driver.get(URL)
def searchClick():
CampBtn = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[#alt='PONDEROSA CAMPGROUND']"))).click()
elem = driver.find_element_by_xpath("//a[#alt='PONDEROSA CAMPGROUND']")
if elem.isDisplayed():
print(elem)
elem.click()
else:
print("no availability")
searchCondition()
time.sleep(20)
You have to point your xpath to the interactive element for example a, input, button
And have to induce some explicit wait with expected condition to check that the element is all set to perform click operation. For example -
WebDriverWait(web, 20).until(EC.element_to_be_clickable((By.XPATH,"//a[#alt='PONDEROSA CAMPGROUND']")))
elem = web.find_element_by_xpath("//a[#alt='PONDEROSA CAMPGROUND']")
elem.click()
After clicking on the image it opens up new tab as below. To see this operation add some wait for sometime after click operation -
Background:
I have the following HTML code that I am trying to get a XPATH to use with Selenium
<div class="btn-group">
<a type="button" class="btn btn-primary btn-user" onclick="AiD('182030801')" href="/download.pdf?id=182030801&h=917901e6659ad5eb53970aecf687b53e&u=cache&ext=pdf" target="_blank" style="border-top-left-radius: 3px;border-bottom-left-radius: 3px;">
<i class="fas fa-cloud-download-alt" aria-hidden="true" style="margin-right: 9px;margin-left: 2px;font-size: 25px;vertical-align: middle;color: #119802;"></i>Download ( PDF )
</a>
[...]
</div>
Code:
What have I tried to do with Python's Selenium is the following, however I cannot quiet get it to work without Python throwing an error:
browser.find_element(By.XPATH, "//div[#class='btn-group']/a").click()
browser.find_element(By.XPATH, "//div[#class='btn tn-primary btn-user']").click()
Error:
When I run the above snippet of code, the following error is produced and script crashes thereafter:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn tn-primary btn-user']"}
Problem:
The error states "no such element", however the element clearly exists "class="btn btn-primary btn-user""
Question:
How can I use Seleniums XPATH to "see" class="btn btn-primary btn-user" and click it to download a PDF?
Link:
PDFDrive
USe Xpath //div/a[#class='btn btn-primary btn-user']
Make sure the intended element is not under an iFrame. If it is then first you need to switch into that iFrame and then have to perform the action
Make sure you are using proper synchrnization and your element is ready. Introduce explicit wait as below:
driver.get('https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='btn-group']/a"))).click()
time.sleep(10) // it doesn't recommanded to put hardcoded wait but for debugging purpose you can check
Import below packages for this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
You are using "div" tag but I can see respective class = btn btn-primary btn-user is inside "a" tag
try below code :
browser.find_element(By.XPATH, "//a[#class='btn tn-primary btn-user']").click()
Or you can go with another locator as well.
CSS : a.btn.btn-primarybtn-user
So you need to wait for the element to be clickable. Triggered the download.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
url = 'https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html'
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.btn.btn-primary.btn-user'))).click()
I am trying to get the snapshot URL from the snapshot window that opens when ALT + S is pressed. I can save the image. but instead i want to get the URL that is in the snapshot box and save it in a variable
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC1
from selenium.webdriver.common.by import By
driver = webdriver.Chrome('C:\\Windows\\chromedriver.exe') # change as per your location
driver.get ("https://www.tradingview.com/chart/1pPtW0th/")
driver.maximize_window()
ActionChains(driver).key_down(Keys.ALT).send_keys('s').perform()
wait_time = 25 # a very long wait time
#want to get snapshot URL here, instead of saving it.
element = WebDriverWait(driver, wait_time).until(EC1.element_to_be_clickable((By.LINK_TEXT, 'Save image')))[enter image description here][1]
time.sleep(3)
driver.close()
You could try targeting the following <div> element and extract the attribute "data-clipboard-text":
<div data-clipboard-text="https://www.tradingview.com/x/ODEmfXoU/" class="copyBtn-1oB8tEqo- shadow-2JTdO0Fb-">
<button class="button-2O-nMUcz- active-2UxWxOgk- withPadding-_5CJoO5q- ghost-3yO24wIn- primary-1rSzOFdX-" tabindex="0" title="" target="" href="">
<span class="hiddenText-3qcN5Wif-">Copy</span>
<span class="text-2KOWx3rB-">Copy</span>
</button>
</div>